Skip to content

Commit

Permalink
emit middleware.mjs only when user middleware exists
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Jan 25, 2024
1 parent ad1a731 commit 2e6008a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
3 changes: 1 addition & 2 deletions packages/astro/src/core/app/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export function deserializeManifest(serializedManifest: SerializedSSRManifest):
const clientDirectives = new Map(serializedManifest.clientDirectives);

return {
// serialized manifest will always contain the middleware (see plugin-ssr.ts)
// this is expected to be overwritten, but is present for types
// in case user middleware exists, this no-op middleware will be reassigned (see plugin-ssr.ts)
middleware(_, next) { return next() },
...serializedManifest,
assets,
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
const baseDirectory = getOutputDirectory(opts.settings.config);
const renderersEntryUrl = new URL('renderers.mjs', baseDirectory);
const renderers = await import(renderersEntryUrl.toString());
const { onRequest: middleware } = await import(new URL('middleware.mjs', baseDirectory).toString());
let middleware: MiddlewareHandler = (_, next) => next();
try {
middleware = await import(new URL('middleware.mjs', baseDirectory).toString()).then(mod => mod.onRequest);
} catch {}
manifest = createBuildManifest(
opts.settings,
internals,
Expand Down
21 changes: 8 additions & 13 deletions packages/astro/src/core/middleware/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { addRollupInput } from '../build/add-rollup-input.js';
import type { BuildInternals } from '../build/internal.js';
import type { StaticBuildOptions } from '../build/types.js';
import { MIDDLEWARE_PATH_SEGMENT_NAME } from '../constants.js';
import type { PluginContext } from 'rollup';

export const MIDDLEWARE_MODULE_ID = '\0astro-internal:middleware';
const NOOP_MIDDLEWARE = '\0noop-middleware';
Expand Down Expand Up @@ -45,12 +44,17 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }):
},
async load(id) {
if (id === NOOP_MIDDLEWARE) {
// In the build, tell Vite to emit this file
if (isCommandBuild) emit(this, id)
return 'export const onRequest = (_, next) => next()';
} else if (id === MIDDLEWARE_MODULE_ID) {
// In the build, tell Vite to emit this file
if (isCommandBuild) emit(this, id)
if (isCommandBuild) {
this.emitFile({
type: 'chunk',
preserveSignature: 'strict',
fileName: 'middleware.mjs',
id,
})
}

const preMiddleware = createMiddlewareImports(settings.middlewares.pre, 'pre');
const postMiddleware = createMiddlewareImports(settings.middlewares.post, 'post');
Expand All @@ -77,15 +81,6 @@ export const onRequest = sequence(
};
}

function emit(context: PluginContext, id: string) {
return context.emitFile({
type: 'chunk',
preserveSignature: 'strict',
fileName: 'middleware.mjs',
id,
})
}

function createMiddlewareImports(
entrypoints: string[],
prefix: string
Expand Down

0 comments on commit 2e6008a

Please sign in to comment.