Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 5, 2022
1 parent 1ed028a commit 643f664
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions packages/kit/src/runtime/server/page/serialize_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ export function serialize_data(fetched, filter, prerendering = false) {
/** @type {Record<string, string>} */
const headers = {};

let cache_control = null;
let age = null;

for (const [key, value] of fetched.response.headers) {
const lower = key.toLowerCase();
if (filter(lower, value)) {
headers[lower] = value;
if (filter(key, value)) {
headers[key] = value;
}

if (key === 'cache-control') cache_control = value;
if (key === 'age') age = value;
}

const payload = {
Expand All @@ -70,16 +75,11 @@ export function serialize_data(fetched, filter, prerendering = false) {
attrs.push(`data-hash=${escape_html_attr(hash(fetched.request_body))}`);
}

if (!prerendering && fetched.method === 'GET') {
const cache_control = headers['cache-control'];
if (cache_control) {
const match = /s-maxage=(\d+)/g.exec(cache_control) ?? /max-age=(\d+)/g.exec(cache_control);
if (match) {
const age = headers['age'] ?? '0';

const ttl = +match[1] - +age;
attrs.push(`data-ttl="${ttl}"`);
}
if (!prerendering && fetched.method === 'GET' && cache_control) {
const match = /s-maxage=(\d+)/g.exec(cache_control) ?? /max-age=(\d+)/g.exec(cache_control);
if (match) {
const ttl = +match[1] - +(age ?? '0');
attrs.push(`data-ttl="${ttl}"`);
}
}

Expand Down

0 comments on commit 643f664

Please sign in to comment.