Skip to content

Commit 19667c4

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 e632c09 commit 19667c4

File tree

14 files changed

+106
-8
lines changed

14 files changed

+106
-8
lines changed

.changeset/smooth-tables-tan.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'astro': patch
3+
'@astrojs/netlify': patch
4+
---
5+
6+
Netlify Edge: Forward requests for static assets

packages/astro/src/core/app/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ export function deserializeManifest(serializedManifest: SerializedSSRManifest):
1313
route.routeData = deserializeRouteData(serializedRoute.routeData);
1414
}
1515

16+
const assets = new Set<string>(serializedManifest.assets);
17+
1618
return {
1719
...serializedManifest,
20+
assets,
1821
routes,
1922
};
2023
}

packages/astro/src/core/app/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ export interface SSRManifest {
2626
pageMap: Map<ComponentPath, ComponentInstance>;
2727
renderers: SSRLoadedRenderer[];
2828
entryModules: Record<string, string>;
29+
assets: Set<string>;
2930
}
3031

31-
export type SerializedSSRManifest = Omit<SSRManifest, 'routes'> & {
32+
export type SerializedSSRManifest = Omit<SSRManifest, 'routes' | 'assets'> & {
3233
routes: SerializedRouteInfo[];
34+
assets: string[];
3335
};
3436

3537
export type AdapterCreateExports<T = any> = (

packages/astro/src/core/build/static-build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { fileURLToPath } from 'url';
1010
import * as vite from 'vite';
1111
import { createBuildInternals } from '../../core/build/internal.js';
1212
import { info } from '../logger/core.js';
13-
import { appendForwardSlash, prependForwardSlash } from '../../core/path.js';
13+
import { prependForwardSlash } from '../../core/path.js';
1414
import { emptyDir, removeDir } from '../../core/util.js';
1515
import { rollupPluginAstroBuildCSS } from '../../vite-plugin-build-css/index.js';
1616
import { vitePluginHoistedScripts } from './vite-plugin-hoisted-scripts.js';

packages/astro/src/core/build/vite-plugin-ssr.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type { SerializedRouteInfo, SerializedSSRManifest } from '../app/types';
77
import { serializeRouteData } from '../routing/index.js';
88
import { eachPageData } from './internal.js';
99
import { addRollupInput } from './add-rollup-input.js';
10+
import { fileURLToPath } from 'url';
11+
import glob from 'fast-glob';
1012
import { virtualModuleId as pagesVirtualModuleId } from './vite-plugin-pages.js';
1113
import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
1214

@@ -65,11 +67,19 @@ if(_start in adapter) {
6567
}
6668
return void 0;
6769
},
68-
generateBundle(_opts, bundle) {
69-
const manifest = buildManifest(buildOpts, internals);
70+
async generateBundle(_opts, bundle) {
71+
const staticFiles = await glob('**/*', {
72+
cwd: fileURLToPath(buildOpts.buildConfig.client),
73+
});
74+
75+
const manifest = buildManifest(buildOpts, internals, staticFiles);
76+
77+
7078

7179
for (const [_chunkName, chunk] of Object.entries(bundle)) {
72-
if (chunk.type === 'asset') continue;
80+
if (chunk.type === 'asset') {
81+
continue;
82+
};
7383
if (chunk.modules[resolvedVirtualModuleId]) {
7484
const code = chunk.code;
7585
chunk.code = code.replace(replaceExp, () => {
@@ -81,7 +91,7 @@ if(_start in adapter) {
8191
};
8292
}
8393

84-
function buildManifest(opts: StaticBuildOptions, internals: BuildInternals): SerializedSSRManifest {
94+
function buildManifest(opts: StaticBuildOptions, internals: BuildInternals, staticFiles: string[]): SerializedSSRManifest {
8595
const { astroConfig } = opts;
8696

8797
const routes: SerializedRouteInfo[] = [];
@@ -112,6 +122,7 @@ function buildManifest(opts: StaticBuildOptions, internals: BuildInternals): Ser
112122
pageMap: null as any,
113123
renderers: [],
114124
entryModules,
125+
assets: staticFiles.map(s => '/' + s)
115126
};
116127

117128
return ssrManifest;

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+
}

0 commit comments

Comments
 (0)