0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

fix(pipeline): retrieveRoutesToGenerate for ssr

This commit is contained in:
Goulven Clec'h 2024-04-24 21:03:56 +02:00
parent 73ba1518c6
commit 96a6518aba
3 changed files with 33 additions and 7 deletions

View file

@ -185,7 +185,18 @@ export class BuildPipeline extends Pipeline {
virtualModulePageName.includes(ASTRO_PAGE_RESOLVED_MODULE_ID) ||
virtualModulePageName.includes(RESOLVED_SPLIT_MODULE_ID)
) {
const pageKey = getPageKeyFromVirtualModulePageName(ASTRO_PAGE_RESOLVED_MODULE_ID, virtualModulePageName)
let pageKey;
if (virtualModulePageName.includes(ASTRO_PAGE_RESOLVED_MODULE_ID)) {
pageKey = getPageKeyFromVirtualModulePageName(
ASTRO_PAGE_RESOLVED_MODULE_ID,
virtualModulePageName
);
} else {
pageKey = getPageKeyFromVirtualModulePageName(
RESOLVED_SPLIT_MODULE_ID,
virtualModulePageName
);
}
const pageData = this.internals.pagesByKeys.get(pageKey);
if (!pageData) {

View file

@ -13,7 +13,7 @@ import { SSR_MANIFEST_VIRTUAL_MODULE_ID } from './plugin-manifest.js';
import { MIDDLEWARE_MODULE_ID } from './plugin-middleware.js';
import { ASTRO_PAGE_MODULE_ID } from './plugin-pages.js';
import { RENDERERS_MODULE_ID } from './plugin-renderers.js';
import { getPathFromVirtualModulePageName, getVirtualModulePageName } from './util.js';
import { getPathFromVirtualModulePageName, getVirtualModulePageName, virtualModuleNameFromResolvedId } from './util.js';
export const SSR_VIRTUAL_MODULE_ID = '@astrojs-ssr-virtual-entry';
export const RESOLVED_SSR_VIRTUAL_MODULE_ID = '\0' + SSR_VIRTUAL_MODULE_ID;
@ -172,13 +172,9 @@ function vitePluginSSRSplit(
const imports: string[] = [];
const contents: string[] = [];
const exports: string[] = [];
// TODO: Change that broken usage of getVirtualModulePageName
// I should refactor getPathFromVirtualModulePageName
const path = getPathFromVirtualModulePageName(RESOLVED_SPLIT_MODULE_ID, id);
const virtualModuleName = getVirtualModulePageName(ASTRO_PAGE_MODULE_ID, path, "");
const virtualModuleName = virtualModuleNameFromResolvedId(ASTRO_PAGE_MODULE_ID, RESOLVED_SPLIT_MODULE_ID, id);
let module = await this.resolve(virtualModuleName);
if (module) {
// we need to use the non-resolved ID in order to resolve correctly the virtual module
imports.push(`import * as pageModule from "${virtualModuleName}";`);
}
const middleware = await this.resolve(MIDDLEWARE_MODULE_ID);

View file

@ -72,6 +72,25 @@ export function getVirtualModulePageName(virtualModulePrefix: string, path: stri
);
}
/**
* In SSR plugins, we need to use the non-resolved virtualModuleName in order to resolve correctly the virtual module.
* @param virtualModulePrefix The prefix used to create the virtual module
* @param resolvedModulePrefix The prefix of the resolved virtual module
* @param resolvedId The resolved virtual module id
* @returns
*/
export function virtualModuleNameFromResolvedId(virtualModulePrefix: string, resolvedModulePrefix: string, resolvedId: string) {
const extension = extname(resolvedId);
const clean_path = resolvedId.slice(resolvedModulePrefix.length);
return (
virtualModulePrefix +
(extension.startsWith('.')
? clean_path.slice(0, -extension.length) + extension.replace('.', ASTRO_PAGE_EXTENSION_POST_PATTERN)
: clean_path
)
);
}
/**
* From the VirtualModulePageName, get the original pageData key.
* @param virtualModulePrefix The prefix used to create the virtual module