Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@astrojs/sitemap: Fixes generated URLs when using a base with a SSR adapter #9704

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-seals-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/sitemap": patch
---

Fixes generated URLs when using a `base` with a SSR adapter
11 changes: 6 additions & 5 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
*/
if (r.pathname) {
if (isStatusCodePage(r.pathname ?? r.route)) return urls;
/**
* remove the initial slash from relative pathname
* because `finalSiteUrl` always has trailing slash
*/
const fullPath = finalSiteUrl.pathname + r.generate(r.pathname).substring(1);

// `finalSiteUrl` may end with a trailing slash
// or not because of base paths.
let fullPath = finalSiteUrl.pathname;
if (fullPath.endsWith('/')) fullPath += r.generate(r.pathname).substring(1);
else fullPath += r.generate(r.pathname);

let newUrl = new URL(fullPath, finalSiteUrl).href;

Expand Down
39 changes: 39 additions & 0 deletions packages/integrations/sitemap/test/base-path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { loadFixture, readXML } from './test-utils.js';
import { expect } from 'chai';

describe('URLs with base path', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

describe('using node adapter', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr/',
base: '/base',
});
await fixture.build();
});

it('Base path is concatenated correctly', async () => {
const data = await readXML(fixture.readFile('/client/sitemap-0.xml'));
const urls = data.urlset.url;
expect(urls[0].loc[0]).to.equal('http://example.com/base/one/');
});
});

describe('static', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/static/',
base: '/base',
});
await fixture.build();
});

it('Base path is concatenated correctly', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url;
expect(urls[0].loc[0]).to.equal('http://example.com/base/123/');
});
});
});