fix: generated redirect page canonical lacks of site prefix (#8591)

This commit is contained in:
Rishi Raj Jain 2023-09-25 13:24:58 +05:30 committed by GitHub
parent 2365c12464
commit 863f5171e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
add site url to the location of redirect

View file

@ -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

View file

@ -9,4 +9,7 @@ export default defineConfig({
ssr: {
noExternal: ['@test/static-build-pkg'],
},
redirects: {
'/old': '/new',
},
});

View file

@ -0,0 +1 @@
<p></p>

View file

@ -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');