Skip to content

Commit

Permalink
Netlify Edge: forward requests for static assets (withastro#3170)
Browse files Browse the repository at this point in the history
* Netlify Edge: forward requests for static assets

* Adds a changeset

* Don't run edge tests, yet
  • Loading branch information
matthewp authored Apr 22, 2022
1 parent 59edc46 commit ec9b4a9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/core/app/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export function deserializeManifest(serializedManifest: SerializedSSRManifest):
route.routeData = deserializeRouteData(serializedRoute.routeData);
}

const assets = new Set<string>(serializedManifest.assets);

return {
...serializedManifest,
assets,
routes,
};
}
4 changes: 3 additions & 1 deletion src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ export interface SSRManifest {
pageMap: Map<ComponentPath, ComponentInstance>;
renderers: SSRLoadedRenderer[];
entryModules: Record<string, string>;
assets: Set<string>;
}

export type SerializedSSRManifest = Omit<SSRManifest, 'routes'> & {
export type SerializedSSRManifest = Omit<SSRManifest, 'routes' | 'assets'> & {
routes: SerializedRouteInfo[];
assets: string[];
};

export type AdapterCreateExports<T = any> = (
Expand Down
2 changes: 1 addition & 1 deletion src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { fileURLToPath } from 'url';
import * as vite from 'vite';
import { createBuildInternals } from '../../core/build/internal.js';
import { info } from '../logger/core.js';
import { appendForwardSlash, prependForwardSlash } from '../../core/path.js';
import { prependForwardSlash } from '../../core/path.js';
import { emptyDir, removeDir } from '../../core/util.js';
import { rollupPluginAstroBuildCSS } from '../../vite-plugin-build-css/index.js';
import { vitePluginHoistedScripts } from './vite-plugin-hoisted-scripts.js';
Expand Down
19 changes: 15 additions & 4 deletions src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { SerializedRouteInfo, SerializedSSRManifest } from '../app/types';
import { serializeRouteData } from '../routing/index.js';
import { eachPageData } from './internal.js';
import { addRollupInput } from './add-rollup-input.js';
import { fileURLToPath } from 'url';
import glob from 'fast-glob';
import { virtualModuleId as pagesVirtualModuleId } from './vite-plugin-pages.js';
import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';

Expand Down Expand Up @@ -65,11 +67,19 @@ if(_start in adapter) {
}
return void 0;
},
generateBundle(_opts, bundle) {
const manifest = buildManifest(buildOpts, internals);
async generateBundle(_opts, bundle) {
const staticFiles = await glob('**/*', {
cwd: fileURLToPath(buildOpts.buildConfig.client),
});

const manifest = buildManifest(buildOpts, internals, staticFiles);



for (const [_chunkName, chunk] of Object.entries(bundle)) {
if (chunk.type === 'asset') continue;
if (chunk.type === 'asset') {
continue;
};
if (chunk.modules[resolvedVirtualModuleId]) {
const code = chunk.code;
chunk.code = code.replace(replaceExp, () => {
Expand All @@ -81,7 +91,7 @@ if(_start in adapter) {
};
}

function buildManifest(opts: StaticBuildOptions, internals: BuildInternals): SerializedSSRManifest {
function buildManifest(opts: StaticBuildOptions, internals: BuildInternals, staticFiles: string[]): SerializedSSRManifest {
const { astroConfig } = opts;

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

return ssrManifest;
Expand Down

0 comments on commit ec9b4a9

Please sign in to comment.