Skip to content

Commit

Permalink
fix: generated redirect page canonical lacks of site prefix (#8591)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishi Raj Jain authored Sep 25, 2023
1 parent 2365c12 commit 863f517
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-oranges-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

add site url to the location of redirect
6 changes: 4 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/static-build/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export default defineConfig({
ssr: {
noExternal: ['@test/static-build-pkg'],
},
redirects: {
'/old': '/new',
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p></p>
8 changes: 8 additions & 0 deletions packages/astro/test/static-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 863f517

Please sign in to comment.