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

feat: sitemap:input Nitro hook #397

Merged
merged 1 commit into from
Jan 15, 2025
Merged

feat: sitemap:input Nitro hook #397

merged 1 commit into from
Jan 15, 2025

Conversation

harlan-zw
Copy link
Collaborator

@harlan-zw harlan-zw commented Jan 15, 2025

πŸ”— Linked issue

#396

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

For users relying on Nitro hooks to add new URLs they were currently limited to the sitemap:resolved hook.

This hook mostly worked but required for end users to provide a "resolved" URL, to do so in user-land is not easy.

This PR firstly fixes it so users can add new URLs via this hook and they will get properly normalized, however this is still buggy as they are missing important resolving steps and the types will be broken.

To get around these issues, a new hook is introduced called sitemap:input that is called immediately after the URLs after resolved from the sources. This hook takes in "input" type URLs so that can be just a string or an object that will be fully resolved.

'sitemap:input'

Type: async (ctx: { urls: SitemapUrlInput[]; sitemapName: string }) => void | Promise<void>{lang="ts"}

Triggers once the raw list of URLs is collected from sources.

This hook is best used for inserting new URLs into the sitemap.

import { defineNitroPlugin } from 'nitropack/runtime'

export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('sitemap:resolved', async (ctx) => {
    // SitemapUrlInput is either a string 
    ctx.urls.push('/foo')
    // or an object with loc, changefreq, and priority
    ctx.urls.push({
      loc: '/bar',
      changefreq: 'daily',
      priority: 0.8,
    })
  })
})

Copy link

vercel bot commented Jan 15, 2025

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Updated (UTC)
sitemap-edge-demo ❌ Failed (Inspect) Jan 15, 2025 1:53am

@harlan-zw harlan-zw merged commit e4a987c into main Jan 15, 2025
1 of 2 checks passed
@harlan-zw harlan-zw deleted the feat/sitemap-input-hook branch January 15, 2025 02:11
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.

1 participant