Fix injectRoute
(#7943)
* fix: inject route! hack!
* refactor: use integration container to resolve all injected routes
* chore: add changeset
* Fix pnpm workspace injectRoute bug
See https://github.com/withastro/astro/issues/7561#issuecomment-1620063634
Closes #7561
* Revert "Fix pnpm workspace injectRoute bug"
This reverts commit 3082c7c269
.
* Update .changeset/stupid-pants-press.md
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* Update packages/astro/src/vite-plugin-scripts/page-ssr.ts
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* refactor: cleanup injectedRoute resolution logic
---------
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
This commit is contained in:
parent
1a24ea6b5a
commit
c2682a17c0
5 changed files with 37 additions and 8 deletions
5
.changeset/stupid-pants-press.md
Normal file
5
.changeset/stupid-pants-press.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Ensure that injected routes from `node_modules` are properly detected
|
|
@ -1316,16 +1316,20 @@ export interface AstroUserConfig {
|
|||
*/
|
||||
export type InjectedScriptStage = 'before-hydration' | 'head-inline' | 'page' | 'page-ssr';
|
||||
|
||||
/**
|
||||
* Resolved Astro Config
|
||||
* Config with user settings along with all defaults filled in.
|
||||
*/
|
||||
|
||||
export interface InjectedRoute {
|
||||
pattern: string;
|
||||
entryPoint: string;
|
||||
prerender?: boolean;
|
||||
}
|
||||
|
||||
export interface ResolvedInjectedRoute extends InjectedRoute {
|
||||
resolvedEntryPoint?: URL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolved Astro Config
|
||||
* Config with user settings along with all defaults filled in.
|
||||
*/
|
||||
export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
||||
// Public:
|
||||
// This is a more detailed type than zod validation gives us.
|
||||
|
@ -1414,6 +1418,7 @@ export interface AstroSettings {
|
|||
config: AstroConfig;
|
||||
adapter: AstroAdapter | undefined;
|
||||
injectedRoutes: InjectedRoute[];
|
||||
resolvedInjectedRoutes: ResolvedInjectedRoute[];
|
||||
pageExtensions: string[];
|
||||
contentEntryTypes: ContentEntryType[];
|
||||
dataEntryTypes: DataEntryType[];
|
||||
|
|
|
@ -21,6 +21,7 @@ export function createBaseSettings(config: AstroConfig): AstroSettings {
|
|||
|
||||
adapter: undefined,
|
||||
injectedRoutes: [],
|
||||
resolvedInjectedRoutes: [],
|
||||
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
|
||||
contentEntryTypes: [markdownContentEntryType],
|
||||
dataEntryTypes: [
|
||||
|
|
|
@ -113,8 +113,9 @@ function isInPagesDir(file: URL, config: AstroConfig): boolean {
|
|||
}
|
||||
|
||||
function isInjectedRoute(file: URL, settings: AstroSettings) {
|
||||
for (const route of settings.injectedRoutes) {
|
||||
if (file.toString().endsWith(route.entryPoint)) return true;
|
||||
let fileURL = file.toString();
|
||||
for (const route of settings.resolvedInjectedRoutes) {
|
||||
if (route.resolvedEntryPoint && fileURL === route.resolvedEntryPoint.toString()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
import type { Plugin as VitePlugin } from 'vite';
|
||||
import type { AstroSettings } from '../@types/astro.js';
|
||||
import type { PluginContext } from 'rollup';
|
||||
import type { AstroSettings, InjectedRoute, ResolvedInjectedRoute } from '../@types/astro.js';
|
||||
import type { LogOptions } from '../core/logger/core.js';
|
||||
|
||||
import { normalizePath } from 'vite';
|
||||
import { runHookServerSetup } from '../integrations/index.js';
|
||||
|
||||
/** Connect Astro integrations into Vite, as needed. */
|
||||
|
@ -16,5 +19,19 @@ export default function astroIntegrationsContainerPlugin({
|
|||
configureServer(server) {
|
||||
runHookServerSetup({ config: settings.config, server, logging });
|
||||
},
|
||||
async buildStart() {
|
||||
// Ensure the injectedRoutes are all resolved to their final paths through Rollup
|
||||
settings.resolvedInjectedRoutes = await Promise.all(settings.injectedRoutes.map(route => resolveEntryPoint.call(this, route)))
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveEntryPoint(this: PluginContext, route: InjectedRoute): Promise<ResolvedInjectedRoute> {
|
||||
const resolvedId = await this.resolve(route.entryPoint)
|
||||
.then(res => res?.id)
|
||||
.catch(() => undefined);
|
||||
if (!resolvedId) return route;
|
||||
|
||||
const resolvedEntryPoint = new URL(`file://${normalizePath(resolvedId)}`);
|
||||
return { ...route, resolvedEntryPoint };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue