Skip to content

Commit

Permalink
add logging to server
Browse files Browse the repository at this point in the history
further cache header optimizations in worker
  • Loading branch information
mhuebert committed Nov 7, 2024
1 parent 2882f8f commit 807a8d0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 30 deletions.
67 changes: 42 additions & 25 deletions private-website-cache/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,64 @@ export default {
try {
const cacheInfo = JSON.parse(cacheInfoHeader);
const modifiedResponse = new Response(response.body, response);
const url = new URL(request.url);
const ext = url.pathname.split('.').pop().toLowerCase();

// Clear any existing cache headers that might have been set by IAP
modifiedResponse.headers.delete('Pragma');
modifiedResponse.headers.delete('Cache-Control');
modifiedResponse.headers.delete('Expires');
modifiedResponse.headers.delete('ETag');
modifiedResponse.headers.delete('Last-Modified');
// Clear any existing cache headers
const headersToDelete = [
'Pragma', 'Cache-Control', 'Expires', 'ETag', 'Last-Modified',
'x-cloud-trace-context', 'x-appengine-resource-usage',
'cf-cache-status', 'cf-ray', 'x-powered-by'
];
headersToDelete.forEach(header => modifiedResponse.headers.delete(header));

// Restore cache control headers
const { policy, expires, etag, lastModified } = cacheInfo;
modifiedResponse.headers.set(
'Cache-Control',
`${policy.visibility}, max-age=${policy.maxAge}, stale-while-revalidate=${policy.staleWhileRevalidate}`
);
modifiedResponse.headers.set('Expires', expires);

// Restore validation headers
if (etag) {
modifiedResponse.headers.set('ETag', etag);
}
if (lastModified) {
modifiedResponse.headers.set('Last-Modified', lastModified);
}
const { policy, expires, etag, lastModified, contentLength } = cacheInfo;

// Handle conditional requests
const ifNoneMatch = request.headers.get('If-None-Match');
const ifModifiedSince = request.headers.get('If-Modified-Since');

if ((ifNoneMatch && etag === ifNoneMatch) ||
(ifModifiedSince && lastModified === ifModifiedSince)) {
// Add immutable flag for certain file types
const isImmutable = ['wasm', 'data'].includes(ext);
let cacheControl = `${policy.visibility}, max-age=${policy.maxAge}, stale-while-revalidate=${policy.staleWhileRevalidate}`;
if (isImmutable) {
cacheControl += ', immutable';
}

// Return 304 for conditional requests
if ((etag && ifNoneMatch === etag) ||
(lastModified && ifModifiedSince && new Date(ifModifiedSince) >= new Date(lastModified))) {
return new Response(null, {
status: 304,
headers: new Headers({
'Cache-Control': modifiedResponse.headers.get('Cache-Control'),
'Cache-Control': cacheControl,
'ETag': etag,
'Last-Modified': lastModified
'Last-Modified': lastModified,
'Content-Type': response.headers.get('Content-Type'),
'Vary': 'Accept-Encoding'
})
});
}

// Remove the custom header since we've processed it
// Set cache headers for normal responses
modifiedResponse.headers.set('Cache-Control', cacheControl);
modifiedResponse.headers.set('Expires', expires);
modifiedResponse.headers.set('Vary', 'Accept-Encoding');

// Add compression hint for large files
if (contentLength > 1024 * 1024) {
modifiedResponse.headers.set('Accept-Encoding', 'gzip, deflate, br');
}

if (etag) {
modifiedResponse.headers.set('ETag', etag);
}
if (lastModified) {
modifiedResponse.headers.set('Last-Modified', lastModified);
}

// Remove the custom header
modifiedResponse.headers.delete('X-Cache-Info');
return modifiedResponse;
} catch (e) {
Expand Down
34 changes: 29 additions & 5 deletions src/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,38 @@ const setCacheHeaders = (res, policy, metadata = {}) => {
};

const serveFile = async (res, path) => {
const startTime = Date.now();
try {
console.log(`[${startTime}] Starting file serve for: ${path}`);

const file = default_bucket.file(path);
const metadataStart = Date.now();
const [metadata] = await file.getMetadata();
console.log(`[+${Date.now() - metadataStart}ms] Metadata fetch completed for: ${path} (size: ${metadata.size})`);

setCacheHeaders(res, CACHE_POLICIES.STATIC, metadata);
res.setHeader('Content-Type', metadata.contentType);
res.setHeader('Content-Length', metadata.size);

const streamStart = Date.now();
const stream = file.createReadStream();
stream.on('error', (err) => handleResponseError(res, err));

// Track first data and completion only
let firstChunk = true;
stream.on('data', () => {
if (firstChunk) {
console.log(`[+${Date.now() - streamStart}ms] First chunk received for: ${path}`);
firstChunk = false;
}
});

stream.on('end', () => {
console.log(`[+${Date.now() - startTime}ms] Total time to serve: ${path}`);
});

return stream.pipe(res);
} catch (err) {
console.error(`[+${Date.now() - startTime}ms] Error serving ${path}:`, err);
handleResponseError(res, err);
}
};
Expand Down Expand Up @@ -177,15 +197,17 @@ const serveHtmlWithFallbacks = async (res, parentDomain, subDomain, filePaths) =
};

const handleRequest = async (parentDomain, subDomain, filePath, req, res) => {
const startTime = Date.now();
console.log(`[${startTime}] Request started for: ${req.url}`);

if (req.url.startsWith("/npm/")) {
// workaround for a Quarto issue, https://github.com/quarto-dev/quarto-cli/blob/bee87fb00ac2bad4edcecd1671a029b561c20b69/src/core/jupyter/widgets.ts#L120
// the embed-amd.js script tag should have a `data-jupyter-widgets-cdn-only` attribute (https://www.evanmarie.com/content/files/notebooks/ipywidgets.html)
res.redirect(302, `https://cdn.jsdelivr.net${req.url}`);
return;
// ... existing npm redirect code ...
}

const fileExtension = getExtension(filePath);
try {
console.log(`[+${Date.now() - startTime}ms] Processing ${fileExtension ? fileExtension : 'no-extension'} file: ${filePath}`);

if (fileExtension) {
if (fileExtension === 'html') {
await serveHtml(res, path.join(parentDomain, subDomain, filePath));
Expand All @@ -195,7 +217,9 @@ const handleRequest = async (parentDomain, subDomain, filePath, req, res) => {
} else {
await serveHtmlWithFallbacks(res, parentDomain, subDomain, paths(filePath));
}
console.log(`[+${Date.now() - startTime}ms] Request completed for: ${req.url}`);
} catch (error) {
console.error(`[+${Date.now() - startTime}ms] Error handling request for ${req.url}:`, error);
handleResponseError(res, error);
}
};
Expand Down

0 comments on commit 807a8d0

Please sign in to comment.