Skip to content

Commit

Permalink
perf: simplify route rule logic
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jul 3, 2024
1 parent 5bd02bf commit c02d79f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
20 changes: 5 additions & 15 deletions src/runtime/nitro-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { packString } from 'packrup'
import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin'
import { createRouter as createRadixRouter, toRouteMatcher } from 'radix3'
import { withoutBase } from 'ufo'
import defu from 'defu'
import type { NitroRouteRules } from 'nitropack'
import { createFilter } from './util'
import { debug, exclude, include, mode, replayScript, script } from '#nuxt-delay-hydration/api'
import { useRuntimeConfig } from '#imports'
Expand All @@ -13,21 +9,15 @@ const SCRIPT_REGEX = /<script(.*?)>/g
export default defineNitroPlugin((nitro) => {
const filter = createFilter({ include, exclude })
const config = useRuntimeConfig()
const _routeRulesMatcher = toRouteMatcher(
createRadixRouter({ routes: config.nitro?.routeRules }),
)
nitro.hooks.hook('render:html', (htmlContext, { event }) => {
nitro.hooks.hook('render:html', (htmlContext, ctx) => {
const event = ctx.event
// allow opt-out
if (!filter(event.path))
return

const routeRules = defu({}, ..._routeRulesMatcher.matchAll(
withoutBase(event.path.split('?')[0], useRuntimeConfig().app.baseURL),
).reverse()) as NitroRouteRules

let currentMode = mode
if (typeof routeRules.delayHydration !== 'undefined')
currentMode = routeRules.delayHydration
if (typeof ctx.event.context._nitro?.routeRules?.delayHydration !== 'undefined')
currentMode = ctx.event.context._nitro?.routeRules?.delayHydration

// opt-out
if (!currentMode)
Expand Down Expand Up @@ -81,6 +71,6 @@ export default defineNitroPlugin((nitro) => {
${debug ? 'w._$delayHydration.then((e) => { console.log(\'[nuxt-delay-hydration] Hydration event\', e) })' : ''}
${extraScripts}
})();
</script>`.replace(/\s+/g, ' ').replace(/[\n\r]/g, ''))
</script>`)
})
})
15 changes: 2 additions & 13 deletions src/runtime/nuxt-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import { createRouter as createRadixRouter, toRouteMatcher } from 'radix3'
import defu from 'defu'
import { withoutBase } from 'ufo'
import type { NitroRouteRules } from 'nitropack'
import { createFilter } from './util'
import type { HydrationMode } from './types'
import { exclude, include, mode } from '#nuxt-delay-hydration/api'
import { defineNuxtPlugin, useRequestEvent, useRuntimeConfig, useState } from '#imports'
import { defineNuxtPlugin, useRequestEvent, useState } from '#imports'

export default defineNuxtPlugin(async (nuxtApp) => {

Check failure on line 6 in src/runtime/nuxt-plugin.ts

View workflow job for this annotation

GitHub Actions / ci (18.x, ubuntu-latest)

The inferred type of 'default' cannot be named without a reference to '.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected][email protected]_i_kpp33s6wbhwke3dexshbcwhpte/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.
const hydrationMode = useState<HydrationMode>('nuxt-delay-hydration-mode', () => mode)
if (import.meta.server) {
const event = useRequestEvent()
const config = useRuntimeConfig()
const _routeRulesMatcher = toRouteMatcher(
createRadixRouter({ routes: config.nitro?.routeRules }),
)
const routeRules = defu({}, ..._routeRulesMatcher.matchAll(
withoutBase(event.path.split('?')[0], useRuntimeConfig().app.baseURL),
).reverse()) as NitroRouteRules
if (routeRules.delayHydration)
if (event?.context?._nitro?.routeRules?.delayHydration)
hydrationMode.value = routeRules.delayHydration
}
if (import.meta.client) {
Expand Down

0 comments on commit c02d79f

Please sign in to comment.