-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sitemap): Trailing slashes on root url (#10772)
* add tests that reveal issue * fix trailing slash root page issue * add changeset
- Loading branch information
Showing
7 changed files
with
117 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/sitemap": patch | ||
--- | ||
|
||
Fixes an issue where the root url does not follow the `trailingSlash` config option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { normalize, resolve } from 'path'; | ||
import { createWriteStream, type WriteStream } from 'fs' | ||
import { mkdir } from 'fs/promises'; | ||
import { promisify } from 'util'; | ||
import { Readable, pipeline } from 'stream'; | ||
import replace from 'stream-replace-string' | ||
|
||
import { SitemapAndIndexStream, SitemapStream } from 'sitemap'; | ||
|
||
import type { AstroConfig } from 'astro'; | ||
import type { SitemapItem } from "./index.js"; | ||
|
||
type WriteSitemapConfig = { | ||
hostname: string; | ||
sitemapHostname?: string; | ||
sourceData: SitemapItem[]; | ||
destinationDir: string; | ||
publicBasePath?: string; | ||
limit?: number; | ||
} | ||
|
||
// adapted from sitemap.js/sitemap-simple | ||
export async function writeSitemap({ hostname, sitemapHostname = hostname, | ||
sourceData, destinationDir, limit = 50000, publicBasePath = './', }: WriteSitemapConfig, astroConfig: AstroConfig) { | ||
|
||
await mkdir(destinationDir, { recursive: true }) | ||
|
||
const sitemapAndIndexStream = new SitemapAndIndexStream({ | ||
limit, | ||
getSitemapStream: (i) => { | ||
const sitemapStream = new SitemapStream({ | ||
hostname, | ||
}); | ||
const path = `./sitemap-${i}.xml`; | ||
const writePath = resolve(destinationDir, path); | ||
if (!publicBasePath.endsWith('/')) { | ||
publicBasePath += '/'; | ||
} | ||
const publicPath = normalize(publicBasePath + path); | ||
|
||
let stream: WriteStream | ||
if (astroConfig.trailingSlash === 'never' || astroConfig.build.format === 'file') { | ||
// workaround for trailing slash issue in sitemap.js: https://github.com/ekalinin/sitemap.js/issues/403 | ||
const host = hostname.endsWith('/') ? hostname.slice(0, -1) : hostname | ||
const searchStr = `<loc>${host}/</loc>` | ||
const replaceStr = `<loc>${host}</loc>` | ||
stream = sitemapStream.pipe(replace(searchStr, replaceStr)).pipe(createWriteStream(writePath)) | ||
} else { | ||
stream = sitemapStream.pipe(createWriteStream(writePath)) | ||
} | ||
|
||
return [ | ||
new URL( | ||
publicPath, | ||
sitemapHostname | ||
).toString(), | ||
sitemapStream, | ||
stream, | ||
]; | ||
}, | ||
}); | ||
|
||
let src = Readable.from(sourceData) | ||
const indexPath = resolve( | ||
destinationDir, | ||
`./sitemap-index.xml` | ||
); | ||
return promisify(pipeline)(src, sitemapAndIndexStream, createWriteStream(indexPath)); | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/integrations/sitemap/test/fixtures/trailing-slash/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<title>Index</title> | ||
</head> | ||
<body> | ||
<h1>Index</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.