-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
feat: expose middleware URL to integrations #7458
Changes from all commits
85b06f1
3dd7012
c62ecfa
b918627
392d1ec
5462af5
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Astro exposes the middleware file path to the integrations in the hook `astro:build:done` | ||
|
||
```ts | ||
// myIntegration.js | ||
import type { AstroIntegration } from 'astro'; | ||
function integration(): AstroIntegration { | ||
return { | ||
name: "fancy-astro-integration", | ||
hooks: { | ||
'astro:build:done': ({ middlewareEntryPoint }) => { | ||
if (middlewareEntryPoint) { | ||
// do some operations | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The `middlewareEntryPoint` is only defined if the user has created an Astro middleware. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ const EMPTY_MIDDLEWARE = '\0empty-middleware'; | |
|
||
export function vitePluginMiddleware( | ||
opts: StaticBuildOptions, | ||
_internals: BuildInternals | ||
internals: BuildInternals | ||
): VitePlugin { | ||
return { | ||
name: '@astro/plugin-middleware', | ||
|
@@ -41,6 +41,17 @@ export function vitePluginMiddleware( | |
return 'export const onRequest = undefined'; | ||
} | ||
}, | ||
|
||
writeBundle(_, bundle) { | ||
for (const [chunkName, chunk] of Object.entries(bundle)) { | ||
if (chunk.type === 'asset') { | ||
continue; | ||
} | ||
if (chunk.fileName === 'middleware.mjs') { | ||
internals.middlewareEntryPoint = new URL(chunkName, opts.settings.config.outDir); | ||
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. Should we combine 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. Technically, at this point of the execution, the path is correct. The files are moved later. If we decided to prefix 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. That's fair 👍 I'm mostly concerned that the final constructed path takes a lot of work to do so, which might not be great for perf. I won't let this block the PR though, I'm also fine with this for now. |
||
} | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
|
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.
I will put this string in a shared variable in the following PRs