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

Dynamic Sitemap Copy Plugin (#1232) #1245

Closed

Conversation

jstockdi
Copy link
Contributor

Submitting a Pull Request

Submitting a copy plugin based approach for #1232

Summary of Changes

Adds a package with unit test example

  1. Built plugin
  2. Created test
  3. Added jsdoc

@jstockdi jstockdi self-assigned this Jun 14, 2024
@jstockdi jstockdi requested a review from thescientist13 June 14, 2024 01:48
Copy link
Member

@thescientist13 thescientist13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a comment regarding technical approach, and I think this should also be in core, if we're going to support the static version through core too.

Let's chat during tomorrow's meeting though!

@@ -0,0 +1,30 @@
import fs from 'fs/promises';

const greenwoodPluginDynamicExport = (options = {}) => [{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach and maybe it's just something I'm missing here but the main concern / limitation I see with this approach is that there is no way to programmatically build your own sitemap. To me, the advantage of the dynamic sitemap option was that could automatically build it up whatever way you need, even tapping into your own frontmatter / page metadata.

Referencing my back of the napkin example in #1232, which is basically just taking a page out of the Next.js playbook, I would expect users to be able to make a sitemap.xml.(js|ts) file in the root of their workspace (e.g. src/sitemap.xml.js, which would just export a function in which we pass the compilation to them, and they can build it up themselves and set their own values for all supported properties, since there are things they might want

export async function sitemap(compilation) {
  const urls = compilation.graph.map((page) => {
    const { data, route } = route;

    return `
      <url>
        <loc>http://www.example.com${page.route}</loc>
        <changefreq>${data.freq}</changefreq>
        <priority>https://www.sitemaps.org/protocol.html#prioritydef>${data.priority ?? '1.0'}</priority>
      </url>
    `;
  });
  const sitemap = `
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      ${urls.join('\n')}
    </urlset>
  `;

  return new Response(body, { headers: 'Content-Type' : 'text/xml' })
}

As far as how to do this through greenwood, I think the API Resource as a reference is probably going to get us halfway there at least for development, such that hitting https://localhost:1984/sitemap.xml returns something.

For build time, we maybe add something to the build step like we do for adapters, where if we have a sitemap (perhaps the graph can track this on init?) we import the that function from the user's file and write out the file for them?

@thescientist13 thescientist13 linked an issue Jun 22, 2024 that may be closed by this pull request
@jstockdi jstockdi closed this Jul 21, 2024
@jstockdi jstockdi deleted the 1232-dynamic-sitemap branch July 21, 2024 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sitemap Generation
2 participants