diff --git a/.changeset/great-oranges-pay.md b/.changeset/great-oranges-pay.md new file mode 100644 index 000000000..13b948139 --- /dev/null +++ b/.changeset/great-oranges-pay.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +add site url to the location of redirect diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 02c78c8ae..a5b316554 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -574,7 +574,9 @@ async function generatePath(pathname: string, gopts: GeneratePathOptions, pipeli if (!pipeline.getConfig().build.redirects) { return; } - const location = getRedirectLocationOrThrow(response.headers); + const locationSite = getRedirectLocationOrThrow(response.headers); + const siteURL = pipeline.getConfig().site; + const location = siteURL ? new URL(locationSite, siteURL) : locationSite; const fromPath = new URL(renderContext.request.url).pathname; // A short delay causes Google to interpret the redirect as temporary. // https://developers.google.com/search/docs/crawling-indexing/301-redirects#metarefresh @@ -592,7 +594,7 @@ async function generatePath(pathname: string, gopts: GeneratePathOptions, pipeli } // A dynamic redirect, set the location so that integrations know about it. if (pageData.route.type !== 'redirect') { - pageData.route.redirect = location; + pageData.route.redirect = location.toString(); } } else { // If there's no body, do nothing diff --git a/packages/astro/test/fixtures/static-build/astro.config.mjs b/packages/astro/test/fixtures/static-build/astro.config.mjs index cc4fa3ae9..91cd9d1ca 100644 --- a/packages/astro/test/fixtures/static-build/astro.config.mjs +++ b/packages/astro/test/fixtures/static-build/astro.config.mjs @@ -9,4 +9,7 @@ export default defineConfig({ ssr: { noExternal: ['@test/static-build-pkg'], }, + redirects: { + '/old': '/new', + }, }); diff --git a/packages/astro/test/fixtures/static-build/src/pages/new.astro b/packages/astro/test/fixtures/static-build/src/pages/new.astro new file mode 100644 index 000000000..31ee88261 --- /dev/null +++ b/packages/astro/test/fixtures/static-build/src/pages/new.astro @@ -0,0 +1 @@ +
diff --git a/packages/astro/test/static-build.test.js b/packages/astro/test/static-build.test.js index 54701fee1..8bde08132 100644 --- a/packages/astro/test/static-build.test.js +++ b/packages/astro/test/static-build.test.js @@ -41,6 +41,14 @@ describe('Static build', () => { await fixture.build({ logger }); }); + it('generates canonical redirect page with site prefix', async () => { + const html = await fixture.readFile('/old/index.html'); + const $ = cheerioLoad(html); + const link = $('link[rel="canonical"]'); + const href = link.attr('href'); + expect(href).to.contain('http'); + }); + it('Builds out .astro pages', async () => { const html = await fixture.readFile('/index.html'); expect(html).to.be.a('string');