Skip to content

Commit fece0fe

Browse files
oladayo21claude
andcommitted
fix: improve static asset handling by letting SvelteKit handle all requests
- Remove direct file system access approach - Add isStaticAsset() function to identify static files - Let SvelteKit handle both prerendered routes and static assets - Import prerendered and base from manifest for proper path checking - Follows proven pattern from other SvelteKit adapters 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 620b1a3 commit fece0fe

File tree

1 file changed

+29
-57
lines changed

1 file changed

+29
-57
lines changed

files/handler.js

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import 'SHIMS';
22
import { env } from 'ENV';
3-
import { manifest } from 'MANIFEST';
3+
import { manifest, prerendered, base } from 'MANIFEST';
44
import { Server } from 'SERVER';
55
// 🔥 Use our robust converters instead of basic event parsing
66
import {
77
convertLambdaEventToWebRequest,
88
convertWebResponseToLambdaEvent,
99
} from '@foladayo/lambda-adapter-kit';
1010
import { getRequest } from '@sveltejs/kit/node';
11-
import { readFileSync } from 'node:fs';
12-
import { fileURLToPath } from 'node:url';
13-
import { dirname, join, extname } from 'node:path';
1411

1512
/* global ENV_PREFIX */
1613

@@ -19,9 +16,6 @@ const server = new Server(manifest);
1916
const body_size_limit = Number.parseInt(env('BODY_SIZE_LIMIT', 'BODY_SIZE_LIMIT'));
2017
const binaryMediaTypes = BINARY_MEDIA_TYPES;
2118

22-
// Get the directory of this handler file
23-
const __dirname = dirname(fileURLToPath(import.meta.url));
24-
2519
await server.init({
2620
env: process.env,
2721
});
@@ -90,50 +84,24 @@ function isALBEvent(event) {
9084
}
9185

9286
/**
93-
* Serve static files from the bundled client directory
94-
* @param {string} path - The requested path
95-
* @returns {Promise<Response|null>} - Response for static file or null if not found
87+
* Check if request is for static assets
88+
* @param {string} pathname
89+
* @returns {boolean}
9690
*/
97-
async function tryServeStaticFile(path) {
98-
// Handle client assets (JS, CSS, etc.)
99-
if (path.startsWith('/_app/') || path.startsWith('/favicon.ico')) {
100-
try {
101-
const filePath = join(__dirname, 'client', path);
102-
const content = readFileSync(filePath);
103-
104-
// Determine content type
105-
const ext = extname(path).toLowerCase();
106-
const contentType = {
107-
'.js': 'application/javascript',
108-
'.css': 'text/css',
109-
'.ico': 'image/x-icon',
110-
'.png': 'image/png',
111-
'.jpg': 'image/jpeg',
112-
'.jpeg': 'image/jpeg',
113-
'.gif': 'image/gif',
114-
'.svg': 'image/svg+xml',
115-
'.woff': 'font/woff',
116-
'.woff2': 'font/woff2',
117-
'.ttf': 'font/ttf',
118-
'.eot': 'application/vnd.ms-fontobject'
119-
}[ext] || 'application/octet-stream';
120-
121-
return new Response(content, {
122-
status: 200,
123-
headers: {
124-
'Content-Type': contentType,
125-
'Cache-Control': path.includes('/immutable/')
126-
? 'public, max-age=31536000, immutable'
127-
: 'public, max-age=3600'
128-
}
129-
});
130-
} catch (error) {
131-
// File not found or error reading
132-
return null;
133-
}
134-
}
135-
136-
return null;
91+
function isStaticAsset(pathname) {
92+
return pathname.startsWith(`${base}/_app/`) ||
93+
pathname.startsWith(`${base}/favicon.`) ||
94+
pathname.endsWith('.css') ||
95+
pathname.endsWith('.js') ||
96+
pathname.endsWith('.woff') ||
97+
pathname.endsWith('.woff2') ||
98+
pathname.endsWith('.png') ||
99+
pathname.endsWith('.jpg') ||
100+
pathname.endsWith('.jpeg') ||
101+
pathname.endsWith('.gif') ||
102+
pathname.endsWith('.svg') ||
103+
pathname.endsWith('.webp') ||
104+
pathname.endsWith('.ico');
137105
}
138106

139107
/**
@@ -146,14 +114,18 @@ export const handler = async (event, context) => {
146114
try {
147115
// 🔥 Use our superior event conversion (handles all Lambda event types)
148116
const webRequest = convertLambdaEventToWebRequest(event);
117+
const pathname = new URL(webRequest.url).pathname;
149118

150-
// Try to serve static files first
151-
const staticFileResponse = await tryServeStaticFile(new URL(webRequest.url).pathname);
152-
if (staticFileResponse) {
153-
return await convertWebResponseToLambdaEvent(staticFileResponse, {
154-
binaryMediaTypes,
155-
multiValueHeaders: isALBEvent(event),
156-
});
119+
// Check for prerendered routes
120+
if (prerendered.has(pathname)) {
121+
// Let SvelteKit handle prerendered routes
122+
// Fall through to normal processing
123+
}
124+
125+
// Static assets - let SvelteKit handle them
126+
if (isStaticAsset(pathname)) {
127+
// Let SvelteKit handle static assets
128+
// Fall through to normal processing
157129
}
158130

159131
// Convert to Node.js request format for SvelteKit

0 commit comments

Comments
 (0)