diff --git a/nuxt.config.ts b/nuxt.config.ts index 7dec3551..4e746749 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -95,6 +95,6 @@ export default defineNuxtConfig({ '~/modules/critters', '~/modules/components-chunk', '~/modules/sitemap', - '~/modules/full-static/index', + 'nuxt-full-static', ], }) diff --git a/package.json b/package.json index 6496dc56..de2f41eb 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "@nuxt/kit": "npm:@nuxt/kit-edge@3.0.0-rc.6-27661960.60656df", "critters": "^0.0.16", "prism-themes": "1.9.0", - "vue-plausible": "^1.3.2" + "vue-plausible": "^1.3.2", + "nuxt-full-static": "latest" }, "devDependencies": { "@commitlint/cli": "17.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 91d60f9f..f775c641 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -27,6 +27,7 @@ importers: lint-staged: 13.0.3 magic-regexp: ^0.4.1 nuxt: npm:nuxt3@3.0.0-rc.6-27661960.60656df + nuxt-full-static: latest postcss-html: 1.5.0 prettier: 2.7.1 prism-themes: 1.9.0 @@ -40,6 +41,7 @@ importers: '@nuxt/content': /@nuxt/content-edge/2.1.0-27658086.63749d2 '@nuxt/kit': /@nuxt/kit-edge/3.0.0-rc.6-27661960.60656df critters: 0.0.16 + nuxt-full-static: 0.1.1 prism-themes: 1.9.0 vue-plausible: 1.3.2 devDependencies: @@ -6478,6 +6480,19 @@ packages: fsevents: 2.3.2 dev: true + /nuxt-full-static/0.1.1: + resolution: {integrity: sha512-n2As19bVPerWEn/Le7p0DzSTsAES9S7CKDy+ojZKXhSn0z8NzwldCTZmORYImma/WlF7XTFdB54NHYrUzQlB+w==} + dependencies: + '@nuxt/kit': /@nuxt/kit-edge/3.0.0-rc.6-27661960.60656df + pathe: 0.3.3 + transitivePeerDependencies: + - esbuild + - rollup + - supports-color + - vite + - webpack + dev: false + /nuxt3/3.0.0-rc.6-27661960.60656df: resolution: {integrity: sha512-QSXhWox09tCaKbCg7RHtYn8Y3SNTiKEnrzruwtqcBIk206uITn3STVRWQx2TA4bw8GvNjTqN9CoWwKbB7lMrVw==} engines: {node: ^14.16.0 || ^16.11.0 || ^17.0.0 || ^18.0.0} diff --git a/src/modules/full-static/index.ts b/src/modules/full-static/index.ts deleted file mode 100644 index b53aec3f..00000000 --- a/src/modules/full-static/index.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { fileURLToPath } from 'url' -import { addPlugin, defineNuxtModule, useNuxt } from '@nuxt/kit' -import { join } from 'pathe' -import { createRegExp, exactly } from 'magic-regexp' - -export default defineNuxtModule({ - meta: { - name: 'full-static', - }, - setup() { - const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url)) - - addPlugin({ - src: join(runtimeDir, 'plugin.server.ts'), - mode: 'server', - }) - addPlugin({ - src: join(runtimeDir, 'plugin.client.ts'), - mode: 'client', - }) - - useNuxt().hook('nitro:init', nitro => { - nitro.hooks.hook('prerender:generate', route => { - route.fileName = route.fileName.replace(JSON_SUFFIX_RE, '.json') - }) - }) - }, -}) - -const JSON_SUFFIX_RE = createRegExp(exactly('.json/index.html')) diff --git a/src/modules/full-static/runtime/plugin.client.ts b/src/modules/full-static/runtime/plugin.client.ts deleted file mode 100644 index 3899c2b3..00000000 --- a/src/modules/full-static/runtime/plugin.client.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { NuxtApp } from '#app' -import type { RouteLocationRaw } from 'vue-router' - -type StaticData = Pick - -export default defineNuxtPlugin(nuxtApp => { - const router = useRouter() - const resolve = (route: string | RouteLocationRaw) => - typeof route === 'string' ? route : router.resolve(route).path - - const s = { - prefetched: new Set(), - prefetch: async (route: string | RouteLocationRaw) => { - const path = resolve(route) - - if (!s.prefetched.has(path)) { - const name = path === '/' ? '/index' : path - const { data, state } = await $fetch(`${name}.json`) - Object.assign(nuxtApp.payload.data, data) - for (const item in state) { - nuxtApp.payload.state[item] ??= state[item] - } - } - }, - } - - return { - provide: { static: s }, - } -}) diff --git a/src/modules/full-static/runtime/plugin.server.ts b/src/modules/full-static/runtime/plugin.server.ts deleted file mode 100644 index 51f0ee77..00000000 --- a/src/modules/full-static/runtime/plugin.server.ts +++ /dev/null @@ -1,30 +0,0 @@ -export default defineNuxtPlugin(nuxtApp => { - const router = useRouter() - - router.getRoutes().forEach(route => { - router.addRoute({ - path: (route.path === '/' ? '/index' : route.path) + '.json', - components: route.components, - }) - }) - - nuxtApp.hook('app:rendered', ctx => { - if (ctx.ssrContext.url.endsWith('.json')) { - const res = ctx.ssrContext.res - res.setHeader('Content-Type', 'application/json') - res.end( - JSON.stringify({ - data: nuxtApp.payload.data, - state: { - ...nuxtApp.payload.state, - '$scolor-mode': undefined, - $serror: undefined, - }, - }) - ) - } else { - const url = ctx.ssrContext.url === '/' ? '/index' : ctx.ssrContext.url - ctx.html.head.push(``) - } - }) -})