Skip to content

Commit 9a38333

Browse files
authored
Netlify Edge: forward requests for static assets (#3170)
* Netlify Edge: forward requests for static assets * Adds a changeset * Don't run edge tests, yet
1 parent d0f937a commit 9a38333

File tree

8 files changed

+62
-2
lines changed

8 files changed

+62
-2
lines changed

packages/integrations/netlify/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@astrojs/webapi": "^0.11.1"
3434
},
3535
"devDependencies": {
36+
"@netlify/edge-handler-types": "^0.34.1",
3637
"@netlify/functions": "^1.0.0",
3738
"astro": "workspace:*",
3839
"astro-scripts": "workspace:*"

packages/integrations/netlify/src/netlify-edge-functions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { App } from 'astro/app';
55
export function createExports(manifest: SSRManifest) {
66
const app = new App(manifest);
77

8-
const handler = async (request: Request): Promise<Response> => {
8+
const handler = async (request: Request): Promise<Response | void> => {
9+
const url = new URL(request.url);
10+
11+
// If this matches a static asset, just return and Netlify will forward it
12+
// to its static asset handler.
13+
if(manifest.assets.has(url.pathname)) {
14+
return;
15+
}
916
if (app.match(request)) {
1017
return app.render(request);
1118
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from 'astro/config';
2+
import { netlifyEdgeFunctions } from '@astrojs/netlify';
3+
4+
export default defineConfig({
5+
adapter: netlifyEdgeFunctions({
6+
dist: new URL('./dist/', import.meta.url),
7+
}),
8+
experimental: {
9+
ssr: true
10+
}
11+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@test/netlify-edge-root-dynamic",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*",
7+
"@astrojs/netlify": "workspace:*"
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: blue;
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Testing</title>
4+
<link rel="stylesheet" href="/styles.css">
5+
</head>
6+
<body>
7+
<h1>Testing</h1>
8+
</body>
9+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @ts-ignore
2+
import { runBuild } from './test-utils.ts';
3+
// @ts-ignore
4+
import { assertEquals, assert, DOMParser } from './deps.ts';
5+
6+
// @ts-ignore
7+
Deno.test({
8+
name: 'Assets are preferred over HTML routes',
9+
async fn() {
10+
let close = await runBuild('./fixtures/root-dynamic/');
11+
const { default: handler } = await import('./fixtures/root-dynamic/dist/edge-functions/entry.js');
12+
const response = await handler(new Request('http://example.com/styles.css'));
13+
assertEquals(response, undefined, 'No response because this is an asset');
14+
await close();
15+
},
16+
});

packages/integrations/netlify/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"allowJs": true,
66
"module": "ES2020",
77
"outDir": "./dist",
8-
"target": "ES2020"
8+
"target": "ES2020",
9+
"typeRoots": [
10+
"node_modules/@types",
11+
"node_modules/@netlify"
12+
]
913
}
1014
}

0 commit comments

Comments
 (0)