/ HTTP Headers

Device-Memory

The Device-Memory header is a client hint header that provides information about the device’s memory capacity to the server. This information can be used by servers to optimize content delivery based on the device’s memory constraints. For instance, a server might choose to send a lightweight version of a web page to devices with lower memory capacity, while sending a more feature-rich version to devices with higher memory capacity.

The Device-Memory header value is a decimal floating-point number representing the approximate amount of RAM on the device, in gigabytes (GB). The value is rounded to the nearest power of 2, ensuring that the server receives an approximation of the device’s memory capacity without revealing the exact amount.

Here’s a quick example of a request header that includes the Device-Memory header:

GET /example-page HTTP/1.1
Host: example.com
Device-Memory: 4
Accept: text/html

In this example, the client is informing the server that it has approximately 4 GB of RAM.

Using the Device-Memory Header

To make use of the Device-Memory header in your web application, you first need to ensure that your server is configured to receive and process client hint headers. Once this is done, you can start using the information provided by the header to optimize content delivery.

Let’s consider a scenario where you want to serve different versions of an image based on the device’s memory capacity. You could implement this by checking the Device-Memory header value and selecting an appropriate image file to serve.

Here’s an example of how you might handle this in a Node.js application using the Express framework:

const express = require('express');
const app = express();

app.get('/image', (req, res) => {
  const deviceMemory = parseFloat(req.header('Device-Memory')) || 1;
  
  if (deviceMemory < 2) {
    res.sendFile('path/to/low-res-image.jpg');
  } else if (deviceMemory < 4) {
    res.sendFile('path/to/medium-res-image.jpg');
  } else {
    res.sendFile('path/to/high-res-image.jpg');
  }
});

app.listen(3000, () => console.log('Server listening on port 3000'));

In this example, the server checks the Device-Memory header value and serves a low, medium, or high-resolution image based on the device’s memory capacity.

Response Header Example

When the server processes the request and selects an appropriate resource to serve, it may include the Content-DPR response header to indicate the device pixel ratio for which the resource is optimized. The client can then use this information to properly display the resource.

Here’s an example of a response header that includes the Content-DPR header:

HTTP/1.1 200 OK
Content-Type: image/jpeg
Content-Length: 12345
Content-DPR: 1.5

In this example, the server is informing the client that the served image is optimized for a device pixel ratio of 1.5.

Summary

The Device-Memory HTTP header provides valuable information about a device’s memory capacity, allowing servers to optimize content delivery based on the device’s constraints. By utilizing this header, you can create more efficient and adaptive web applications that cater to a wider range of devices and user experiences.

Was this helpful?

Thanks for your feedback!