From ea0918259964947523827bac6abe88ad3841dbb9 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 11 Dec 2023 10:10:59 +0000 Subject: [PATCH] fix(i18n): correctly pull the ssr entry during build (#9380) --- .changeset/honest-bats-own.md | 5 +++++ .../astro/src/core/build/buildPipeline.ts | 19 +++++++++++++++-- packages/astro/test/i18n-routing.test.js | 21 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 .changeset/honest-bats-own.md diff --git a/.changeset/honest-bats-own.md b/.changeset/honest-bats-own.md new file mode 100644 index 000000000000..3755e7572d8f --- /dev/null +++ b/.changeset/honest-bats-own.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Correctly handle the rendering of i18n routes when `output: "hybrid"` is set diff --git a/packages/astro/src/core/build/buildPipeline.ts b/packages/astro/src/core/build/buildPipeline.ts index 623e89630fcf..166e42a2f8cb 100644 --- a/packages/astro/src/core/build/buildPipeline.ts +++ b/packages/astro/src/core/build/buildPipeline.ts @@ -8,7 +8,10 @@ import { routeIsFallback, routeIsRedirect } from '../redirects/helpers.js'; import { createEnvironment } from '../render/index.js'; import { createAssetLink } from '../render/ssr-element.js'; import type { BuildInternals } from './internal.js'; -import { ASTRO_PAGE_RESOLVED_MODULE_ID } from './plugins/plugin-pages.js'; +import { + ASTRO_PAGE_RESOLVED_MODULE_ID, + getVirtualModulePageNameFromPath, +} from './plugins/plugin-pages.js'; import { RESOLVED_SPLIT_MODULE_ID } from './plugins/plugin-ssr.js'; import { ASTRO_PAGE_EXTENSION_POST_PATTERN } from './plugins/util.js'; import type { PageBuildData, StaticBuildOptions } from './types.js'; @@ -172,7 +175,19 @@ export class BuildPipeline extends Pipeline { (i18nHasFallback(this.getConfig()) || (routeIsFallback(pageData.route) && pageData.route.route === '/')) ) { - pages.set(pageData, path); + // The original component is transformed during the first build, so we have to retrieve + // the actual `.mjs` that was created. + // During the build, we transform the names of our pages with some weird name, and those weird names become the keys of a map. + // The values of the map are the actual `.mjs` files that are generated during the build + + // Here, we take the component path and transform it in the virtual module name + const moduleSpecifier = getVirtualModulePageNameFromPath(path); + // We retrieve the original JS module + const filePath = this.#internals.entrySpecifierToBundleMap.get(moduleSpecifier); + if (filePath) { + // it exists, added it to pages to render, using the file path that we jus retrieved + pages.set(pageData, filePath); + } } } diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index 4b9a032306e3..567dc040e30e 100644 --- a/packages/astro/test/i18n-routing.test.js +++ b/packages/astro/test/i18n-routing.test.js @@ -1380,6 +1380,27 @@ describe('[SSR] i18n routing', () => { }); }); }); + + describe('i18n routing should work with hybrid rendering', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/i18n-routing-prefix-always/', + output: 'hybrid', + adapter: testAdapter(), + }); + await fixture.build(); + app = await fixture.loadTestAdapterApp(); + }); + + it('and render the index page, which is static', async () => { + const html = await fixture.readFile('/client/index.html'); + expect(html).to.include('http-equiv="refresh'); + expect(html).to.include('url=/new-site/en'); + }); + }); }); describe('i18n routing does not break assets and endpoints', () => {