diff options
author | 2023-09-25 13:24:58 +0530 | |
---|---|---|
committer | 2023-09-25 09:54:58 +0200 | |
commit | 863f5171e8e7516c9d72f2e48ea7db1dea71c4f5 (patch) | |
tree | b20f8e8a04282655509801836ddd69b9519c2076 | |
parent | 2365c124645d5067a12987f205cee23a45d1d13d (diff) | |
download | astro-863f5171e8e7516c9d72f2e48ea7db1dea71c4f5.tar.gz astro-863f5171e8e7516c9d72f2e48ea7db1dea71c4f5.tar.zst astro-863f5171e8e7516c9d72f2e48ea7db1dea71c4f5.zip |
fix: generated redirect page canonical lacks of site prefix (#8591)
-rw-r--r-- | .changeset/great-oranges-pay.md | 5 | ||||
-rw-r--r-- | packages/astro/src/core/build/generate.ts | 6 | ||||
-rw-r--r-- | packages/astro/test/fixtures/static-build/astro.config.mjs | 3 | ||||
-rw-r--r-- | packages/astro/test/fixtures/static-build/src/pages/new.astro | 1 | ||||
-rw-r--r-- | packages/astro/test/static-build.test.js | 8 |
5 files changed, 21 insertions, 2 deletions
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 @@ +<p></p> 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'); |