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: add new hook sitemap:prepare #184

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async function generateSitemaps(options, globalCache, nuxtInstance, depth = 0) {
async function generateSitemap(options, globalCache, nuxtInstance, depth = 0) {
// Init options
options = setDefaultSitemapOptions(options, nuxtInstance, depth > 0)
await nuxtInstance.nuxt.callHook('sitemap:prepare', options)

// Init cache
const cache = {}
Expand Down Expand Up @@ -77,6 +78,7 @@ async function generateSitemap(options, globalCache, nuxtInstance, depth = 0) {
async function generateSitemapIndex(options, globalCache, nuxtInstance, depth = 0) {
// Init options
options = setDefaultSitemapIndexOptions(options, nuxtInstance)
await nuxtInstance.nuxt.callHook('sitemap:prepare', options)

// Generate sitemapindex.xml
const base = nuxtInstance.options.router.base
Expand Down
16 changes: 10 additions & 6 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { excludeRoutes } = require('./routes')
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
async function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
/* istanbul ignore if */
if (depth > 1) {
// see https://webmasters.stackexchange.com/questions/18243/can-a-sitemap-index-contain-other-sitemap-indexes
Expand All @@ -27,9 +27,9 @@ function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
const isSitemapIndex = options && options.sitemaps && Array.isArray(options.sitemaps) && options.sitemaps.length > 0

if (isSitemapIndex) {
registerSitemapIndex(options, globalCache, nuxtInstance, depth)
await registerSitemapIndex(options, globalCache, nuxtInstance, depth)
} else {
registerSitemap(options, globalCache, nuxtInstance, depth)
await registerSitemap(options, globalCache, nuxtInstance, depth)
}
}

Expand All @@ -41,11 +41,12 @@ function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
function registerSitemap(options, globalCache, nuxtInstance, depth = 0) {
async function registerSitemap(options, globalCache, nuxtInstance, depth = 0) {
const base = nuxtInstance.options.router.base

// Init options
options = setDefaultSitemapOptions(options, nuxtInstance, depth > 0)
await nuxtInstance.nuxt.callHook('sitemap:prepare', options)

// Init cache
const cache = {}
Expand Down Expand Up @@ -121,11 +122,12 @@ function registerSitemap(options, globalCache, nuxtInstance, depth = 0) {
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
function registerSitemapIndex(options, globalCache, nuxtInstance, depth = 0) {
async function registerSitemapIndex(options, globalCache, nuxtInstance, depth = 0) {
const base = nuxtInstance.options.router.base

// Init options
options = setDefaultSitemapIndexOptions(options, nuxtInstance)
await nuxtInstance.nuxt.callHook('sitemap:prepare', options)

if (options.gzip) {
// Add server middleware for sitemapindex.xml.gz
Expand Down Expand Up @@ -163,7 +165,9 @@ function registerSitemapIndex(options, globalCache, nuxtInstance, depth = 0) {
})

// Register linked sitemaps
options.sitemaps.forEach((sitemapOptions) => registerSitemaps(sitemapOptions, globalCache, nuxtInstance, depth + 1))
await Promise.all(
options.sitemaps.map((sitemapOptions) => registerSitemaps(sitemapOptions, globalCache, nuxtInstance, depth + 1))
)
}

/**
Expand Down
4 changes: 1 addition & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ module.exports = async function module(moduleOptions) {
})

// On "ssr" mode, register middlewares for each sitemap or sitemapindex
options.forEach((options) => {
registerSitemaps(options, globalCache, nuxtInstance)
})
await Promise.all(options.map((options) => registerSitemaps(options, globalCache, nuxtInstance)))
}

async function initOptions(nuxtInstance, moduleOptions) {
Expand Down