fix: generated redirect page canonical lacks of site prefix (#8591)
This commit is contained in:
parent
2365c12464
commit
863f5171e8
5 changed files with 21 additions and 2 deletions
5
.changeset/great-oranges-pay.md
Normal file
5
.changeset/great-oranges-pay.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
add site url to the location of redirect
|
|
@ -574,7 +574,9 @@ async function generatePath(pathname: string, gopts: GeneratePathOptions, pipeli
|
||||||
if (!pipeline.getConfig().build.redirects) {
|
if (!pipeline.getConfig().build.redirects) {
|
||||||
return;
|
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;
|
const fromPath = new URL(renderContext.request.url).pathname;
|
||||||
// A short delay causes Google to interpret the redirect as temporary.
|
// A short delay causes Google to interpret the redirect as temporary.
|
||||||
// https://developers.google.com/search/docs/crawling-indexing/301-redirects#metarefresh
|
// 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.
|
// A dynamic redirect, set the location so that integrations know about it.
|
||||||
if (pageData.route.type !== 'redirect') {
|
if (pageData.route.type !== 'redirect') {
|
||||||
pageData.route.redirect = location;
|
pageData.route.redirect = location.toString();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If there's no body, do nothing
|
// If there's no body, do nothing
|
||||||
|
|
|
@ -9,4 +9,7 @@ export default defineConfig({
|
||||||
ssr: {
|
ssr: {
|
||||||
noExternal: ['@test/static-build-pkg'],
|
noExternal: ['@test/static-build-pkg'],
|
||||||
},
|
},
|
||||||
|
redirects: {
|
||||||
|
'/old': '/new',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
1
packages/astro/test/fixtures/static-build/src/pages/new.astro
vendored
Normal file
1
packages/astro/test/fixtures/static-build/src/pages/new.astro
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<p></p>
|
|
@ -41,6 +41,14 @@ describe('Static build', () => {
|
||||||
await fixture.build({ logger });
|
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 () => {
|
it('Builds out .astro pages', async () => {
|
||||||
const html = await fixture.readFile('/index.html');
|
const html = await fixture.readFile('/index.html');
|
||||||
expect(html).to.be.a('string');
|
expect(html).to.be.a('string');
|
||||||
|
|
Loading…
Reference in a new issue