-
-
Notifications
You must be signed in to change notification settings - Fork 3k
refactor: support middleware in entrypoint #14616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,27 @@ import { App } from './app.js'; | |
| import type { BaseApp } from './base.js'; | ||
| import { DevApp } from './dev/app.js'; | ||
| import { createConsoleLogger } from './logging.js'; | ||
| import type { SSRManifest } from './types.js'; | ||
|
|
||
| const actions = async () => { | ||
| return await import('virtual:astro:actions/entrypoint'); | ||
| }; | ||
| const manifest = Object.assign(serializedManifest, { renderers, actions }); | ||
|
|
||
| const middleware = async () => { | ||
| return await import('virtual:astro:middleware'); | ||
| }; | ||
|
|
||
| const manifest: SSRManifest = Object.assign(serializedManifest, { | ||
| renderers, | ||
| actions, | ||
| middleware, | ||
| routes, | ||
| }); | ||
ematipico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| export function getApp(dev = import.meta.env.DEV): BaseApp { | ||
| if (dev) { | ||
| const logger = createConsoleLogger('debug'); | ||
| return new DevApp(manifest, true, logger, { routes: routes.map((r) => r.routeData) }); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return new DevApp(manifest, true, logger); | ||
| } else { | ||
| return new App(manifest); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import type { Plugin as VitePlugin } from 'vite'; | |
| import { ENTRYPOINT_VIRTUAL_MODULE_ID } from '../../../actions/consts.js'; | ||
| import type { AstroAdapter } from '../../../types/public/integrations.js'; | ||
| import { ASTRO_RENDERERS_MODULE_ID } from '../../../vite-plugin-renderers/index.js'; | ||
| import { MIDDLEWARE_MODULE_ID } from '../../middleware/vite-plugin.js'; | ||
| import { MIDDLEWARE_RESOLVED_MODULE_ID } from '../../middleware/vite-plugin.js'; | ||
| import { routeIsRedirect } from '../../redirects/index.js'; | ||
| import { VIRTUAL_ISLAND_MAP_ID } from '../../server-islands/vite-plugin-server-islands.js'; | ||
| import { addRollupInput } from '../add-rollup-input.js'; | ||
|
|
@@ -101,7 +101,7 @@ function vitePluginSSR( | |
| } | ||
| contents.push(`const pageMap = new Map([\n ${pageMap.join(',\n ')}\n]);`); | ||
| exports.push(`export { pageMap }`); | ||
| const middleware = await this.resolve(MIDDLEWARE_MODULE_ID); | ||
| const middleware = await this.resolve(MIDDLEWARE_RESOLVED_MODULE_ID); | ||
|
||
| const ssrCode = generateSSRCode(adapter, middleware!.id); | ||
| imports.push(...ssrCode.imports); | ||
| contents.push(...ssrCode.contents); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { MiddlewareCantBeLoaded } from '../errors/errors-data.js'; | ||
| import { AstroError } from '../errors/index.js'; | ||
| import type { ModuleLoader } from '../module-loader/index.js'; | ||
| import { MIDDLEWARE_MODULE_ID } from './vite-plugin.js'; | ||
| import { MIDDLEWARE_RESOLVED_MODULE_ID } from './vite-plugin.js'; | ||
|
|
||
| /** | ||
| * It accepts a module loader and the astro settings, and it attempts to load the middlewares defined in the configuration. | ||
|
|
@@ -10,7 +10,7 @@ import { MIDDLEWARE_MODULE_ID } from './vite-plugin.js'; | |
| */ | ||
| export async function loadMiddleware(moduleLoader: ModuleLoader) { | ||
| try { | ||
| return await moduleLoader.import(MIDDLEWARE_MODULE_ID); | ||
| return await moduleLoader.import(MIDDLEWARE_RESOLVED_MODULE_ID); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should still import |
||
| } catch (error: any) { | ||
| const astroError = new AstroError(MiddlewareCantBeLoaded, { cause: error }); | ||
| throw astroError; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { defineMiddleware } from 'astro:middleware' | ||
|
|
||
| export const onRequest = defineMiddleware(async (ctx, next) => { | ||
| const url = new URL(ctx.request.url); | ||
|
|
||
| if (url.pathname.includes("/mid/to-redirect")) { | ||
| return ctx.redirect("/mid/from-redirect"); | ||
| } | ||
| if (url.pathname.includes("/mid/to-rewrite")) { | ||
| return ctx.rewrite("/mid/from-rewrite"); | ||
| } | ||
|
|
||
|
|
||
| ctx.locals.getGreeting = () => { | ||
| return `Hello from ${url.pathname}`; | ||
| }; | ||
|
|
||
| return next() | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
|
|
||
|
|
||
| <h1>Came from a redirect</h1> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <h1>I am a rewrite</h1> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| const greeting = Astro.locals.getGreeting(); | ||
| --- | ||
|
|
||
| <h1>Greeting from locals</h1> | ||
| <p>{greeting}</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't needed because it's done in base.ts