-
Notifications
You must be signed in to change notification settings - Fork 10
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
Sitemap Generation #1232
Comments
For now a couple ways to implement this manually could be to:
|
For 2, would it be a copy plugin? ie, the plugin would generate a temporary file, then pass
|
@jstockdi So after running // sitemap-gen.js
import fs from 'fs';
import graph from './public/graph.json' with { type: 'json'};
const urls = graph.map((page) => {
return `
<url>
<loc>http://www.example.com${page.route}</loc>
</url>
`
}).join('\n');
fs.writeFileSync('./public/sitemap.xml', `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls}
</urlset>
`); # after running Greenwood build, or add to your npm scripts...
$ node sitemap-gen.js edit: sorry, I think you were referencing option 1, in which case yes, a copy plugin would do the trick, e.g. function myCopySitemapPlugin() {
return {
type: 'copy',
name: 'plugin-copy-sitemap',
provider: (compilation) => {
const filename = 'sitemap.xml';
const { userWorkspace, outputDir } = compilation.context;
return [{
from: new URL('./${filename}', userWorkspace),
to: new URL('./${filename}', outputDir)
}];
}
};
} Otherwise, to generate dynamically for now, the above script sample should also work. 🎯 |
Actually, I was thinking use a copy plugin... Read the graph, write a dynamic file to scratch, then copy to final. const greenwoodPluginSitemap = [{
type: 'copy',
name: 'plugin-copy-sitemap',
provider: async (compilation) => {
const { outputDir, scratchDir } = compilation.context;
const urls = graph.map((page) => {
return `
<url>
<loc>http://www.example.com${page.route}</loc>
</url>
`
}).join('\n');
const sitemapFromUrl = new URL(`./sitemap.xml`, scratchDir)
fs.writeFileSync(
sitemapFromUrl, `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls}
</urlset>
`);
const assets = [];
assets.push({
from: sitemapFromUrl,
to: new URL(`./${fileName}`, outputDir)
});
return assets;
}
}]; |
So for the two different options here from a contributing perspective, here are my initial thoughts Static SitemapFor a static sitemap in the root workspace folder, e.g. src/sitemap.xml it should just be as simple as following one of the existing "copy" based features / plugins, like our robots.txt plugin Dynamic SitemapAs for supporting a dynamic flavor of this, e.g. src/sitemap.xml.js I'm not sure I have an idea on the best way to instrument this off the top of my head, mainly for handling development vs production workflows which are slightly different. For development, we could make a resource plugin that resource plugin that has a serve lifecycle that checks if the dynamic flavor exists in async function shouldServe(url) {
return url.pathname.endsWith('sitemap.xml.js')
}
async function serve(url) {
const { generateSitemap } = (await import(url)).then(module => module);
const sitemap = await generateSitemap(this.compilation);
return new Response(sitemap, { headers: { 'Content-Type': 'text/xml' });
} For production, we could probably just run that similar logic in serve (except just outputting a file instead of returning a TestingGreenwood tests are basically black box tests, You can create an exact version of any greenwood project + config, run the CLI, and just the output, in either case, that a sitemap.xml file is generated in the output folder. We would probably want on test case for each of static and dynamic sitemaps DocumentationI think for now the best place to document these would probably be in the Styles and Assets page |
* #1232 - Adding default static sitemap plugin * 1232 - Adding meta files documentation * update meta files docs and usage examples --------- Co-authored-by: Owen Buckley <[email protected]>
* #1232 - Adding default static sitemap plugin * 1232 - Adding meta files documentation * update meta files docs and usage examples --------- Co-authored-by: Owen Buckley <[email protected]>
Summary
Called out in our Slack channel, but Greenwood should definitely have some support for sitemaps, which are an XML file used to tell Search Engines about the content and pages contained within a site, in particular for larger sites and / or where links between pages are maybe not as consistent.
https://developers.google.com/search/docs/crawling-indexing/sitemaps/overview
Here is a basic example
https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap
Details
I think the approach used in Next.js is probably good enough for Greenwood supporting either of this options
Might want to wait until after #955 is merged since we might want to piggy back off any solutions there re: extending the ability for pages to be more than just markdown (.md) or JavaScript (.js).
The text was updated successfully, but these errors were encountered: