From d2ed9a3268969b1c65b05d66a4516bd0355b0f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Mon, 29 Jan 2024 14:49:42 +0100 Subject: [PATCH] feat: rollback nuxt-simple-sitemap integration --- eslint.config.js | 6 ++ package.json | 1 + server/routes/sitemap-style.xsl.ts | 19 ++++++ server/routes/sitemap.xml.ts | 94 ++++++++++++++++++++++++++++++ yarn.lock | 68 +++++++++++++-------- 5 files changed, 163 insertions(+), 25 deletions(-) create mode 100644 server/routes/sitemap-style.xsl.ts create mode 100644 server/routes/sitemap.xml.ts diff --git a/eslint.config.js b/eslint.config.js index 588cd3d72..2e430e866 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -10,6 +10,12 @@ export default antfu( 'vue/no-unused-refs': 0, }, }, + { + files: ['**/*.ts'], + rules: { + 'antfu/no-import-node-modules-by-path': 0, + }, + }, { rules: { 'node/prefer-global/process': 0, diff --git a/package.json b/package.json index 8309feb0a..c689d3de0 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "msw": "^1.2.2", "node-sass": "^9.0.0", "nuxt": "^3.2.3", + "nuxt-simple-sitemap": "^2.7.0", "postcss": "^8.4.24", "postcss-html": "^1.6.0", "sass": "^1.63.4", diff --git a/server/routes/sitemap-style.xsl.ts b/server/routes/sitemap-style.xsl.ts new file mode 100644 index 000000000..c174bc44e --- /dev/null +++ b/server/routes/sitemap-style.xsl.ts @@ -0,0 +1,19 @@ +import type { IncomingMessage, ServerResponse } from 'node:http' +import { defineEventHandler } from 'h3' + +import { generateXslStylesheet } from '~/node_modules/nuxt-simple-sitemap/dist/runtime/util/builder' + +// Import by node_modules because access to internal module content + +async function stylesheet( + req: IncomingMessage, + res: ServerResponse, +) { + res.write(generateXslStylesheet()) + res.statusCode = 200 + res.end() +} + +export default defineEventHandler( + async event => await stylesheet(event.node.req, event.node.res), +) diff --git a/server/routes/sitemap.xml.ts b/server/routes/sitemap.xml.ts new file mode 100644 index 000000000..626eee49d --- /dev/null +++ b/server/routes/sitemap.xml.ts @@ -0,0 +1,94 @@ +import type { IncomingMessage, ServerResponse } from 'node:http' +import { defineEventHandler } from 'h3' + +import type { SitemapEntry } from '~/node_modules/nuxt-simple-sitemap/dist/module' +import type { + BuildSitemapOptions, +} from '~/node_modules/nuxt-simple-sitemap/dist/runtime/util/builder' +import { + buildSitemap, +} from '~/node_modules/nuxt-simple-sitemap/dist/runtime/util/builder' +import { getMenu } from '~/lib/apiMenu' +import { getPois } from '~/lib/apiPois' +import { vidos } from '~/lib/config' +import { vidoConfigResolve } from '~/plugins/vido-config' +import type { VidoConfig } from '~/utils/types-config' + +// Import by node_modules because access to internal module content + +async function manifest( + req: IncomingMessage, + res: ServerResponse, +) { + const hostname = (req.headers['x-forwarded-host'] || req.headers.host) as + | string + | undefined + if (hostname) { + const vido: VidoConfig = vidoConfigResolve(hostname, vidos()) + + const menu = getMenu(vido).then(menuItem => + menuItem + .filter(menuItem => menuItem.category && menuItem.id) + .map(menuCategory => ({ + url: `/${menuCategory.id}/`, + })), + ) + + const pois = getPois(vido).then(apiPois => + apiPois.features.map(poi => ({ + url: `/poi/${poi.properties.metadata.id}/details`, + lastmod: poi.properties.metadata.updated_at, + })), + ) + + const entries: SitemapEntry[] = (await Promise.all([menu, pois])).flat( + 1, + ) + + entries.push({ + url: '/', + }) + entries.push({ + url: '/embedded/', + }) + + const options: BuildSitemapOptions = { + sitemapConfig: { + dynamicUrlsApiEndpoint: '/__sitemap', + discoverImages: false, + xsl: '/sitemap-style.xsl', + defaults: {}, + enabled: true, + trailingSlash: false, + siteUrl: `https://${hostname}`, + autoLastmod: false, + inferStaticPagesAsRoutes: false, + // sitemaps?: boolean | Record>; + hasApiRoutesUrl: false, + hasPrerenderedRoutesPayload: false, + isNuxtContentDocumentDriven: false, + urls: entries, + sitemapName: 'sitemap.xml' + }, + baseURL: `https://${hostname}`, + getRouteRulesForPath(_path: string): Record { + return {} + }, + } + + res.write( + await buildSitemap({...options, sitemapName: 'default'}) + ) + + res.statusCode = 200 + res.end() + } + else { + res.statusCode = 500 + res.end() + } +} + +export default defineEventHandler( + async event => await manifest(event.node.req, event.node.res), +) diff --git a/yarn.lock b/yarn.lock index 1639d8bdd..fe6f5c2bd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2324,6 +2324,31 @@ __metadata: languageName: node linkType: hard +"@nuxt/kit@npm:3.5.3, @nuxt/kit@npm:^3.4.1, @nuxt/kit@npm:^3.5.0, @nuxt/kit@npm:^3.5.3": + version: 3.5.3 + resolution: "@nuxt/kit@npm:3.5.3" + dependencies: + "@nuxt/schema": 3.5.3 + c12: ^1.4.1 + consola: ^3.1.0 + defu: ^6.1.2 + globby: ^13.1.4 + hash-sum: ^2.0.0 + ignore: ^5.2.4 + jiti: ^1.18.2 + knitwork: ^1.0.0 + mlly: ^1.4.0 + pathe: ^1.1.1 + pkg-types: ^1.0.3 + scule: ^1.0.0 + semver: ^7.5.1 + unctx: ^2.3.1 + unimport: ^3.0.7 + untyped: ^1.3.2 + checksum: 0139aa67b4e76f9d23910cec26d3f727413dcdb9e5d5e3e06314256325e0341436651f3b04f0ec3afb3a9b8facca771123fc716adb319f26de16e2610ccd250a + languageName: node + linkType: hard + "@nuxt/kit@npm:^3.0.0-rc.11": version: 3.3.1 resolution: "@nuxt/kit@npm:3.3.1" @@ -2376,31 +2401,6 @@ __metadata: languageName: node linkType: hard -"@nuxt/kit@npm:^3.4.1, @nuxt/kit@npm:^3.5.0, @nuxt/kit@npm:^3.5.3": - version: 3.5.3 - resolution: "@nuxt/kit@npm:3.5.3" - dependencies: - "@nuxt/schema": 3.5.3 - c12: ^1.4.1 - consola: ^3.1.0 - defu: ^6.1.2 - globby: ^13.1.4 - hash-sum: ^2.0.0 - ignore: ^5.2.4 - jiti: ^1.18.2 - knitwork: ^1.0.0 - mlly: ^1.4.0 - pathe: ^1.1.1 - pkg-types: ^1.0.3 - scule: ^1.0.0 - semver: ^7.5.1 - unctx: ^2.3.1 - unimport: ^3.0.7 - untyped: ^1.3.2 - checksum: 0139aa67b4e76f9d23910cec26d3f727413dcdb9e5d5e3e06314256325e0341436651f3b04f0ec3afb3a9b8facca771123fc716adb319f26de16e2610ccd250a - languageName: node - linkType: hard - "@nuxt/schema@npm:3.2.0": version: 3.2.0 resolution: "@nuxt/schema@npm:3.2.0" @@ -3033,6 +3033,7 @@ __metadata: msw: ^1.2.2 node-sass: ^9.0.0 nuxt: ^3.2.3 + nuxt-simple-sitemap: ^2.7.0 opening_hours: ^3.8.0 pinia: ^2.0.33 pinia-shared-state: ^0.4.2 @@ -11423,6 +11424,23 @@ __metadata: languageName: node linkType: hard +"nuxt-simple-sitemap@npm:^2.7.0": + version: 2.7.0 + resolution: "nuxt-simple-sitemap@npm:2.7.0" + dependencies: + "@nuxt/kit": 3.5.3 + chalk: ^5.2.0 + defu: ^6.1.2 + escape-string-regexp: ^5.0.0 + globby: ^13.1.4 + knitwork: ^1.0.0 + pathe: ^1.1.1 + radix3: ^1.0.1 + ufo: ^1.1.2 + checksum: fe072cfc784335fa4c496da4d556b7aa6979f1d64880ce973279227d785a30bf1cb2c4a467bcfe32ed121d6716c726b5d23dd4e962a6494734094ec1bfe3803c + languageName: node + linkType: hard + "nuxt@npm:^3.2.3": version: 3.2.3 resolution: "nuxt@npm:3.2.3"