Skip to content

Commit

Permalink
fix: use runtime config for route rules
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 13, 2023
1 parent f1d8e03 commit 8e59ef9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const clicked = ref(false)
<button :class="{ red: clicked }" style="padding: 30px;" @click="clicked = true">
Big Button
</button>
<NuxtLink to="/not-delayed">
<NuxtLink to="/disabled">
not-delayed
</NuxtLink>
<div>
Expand Down
16 changes: 13 additions & 3 deletions src/runtime/nuxt-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
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, getRouteRules, useRequestEvent, useState } from '#imports'
import { defineNuxtPlugin, useRequestEvent, useRuntimeConfig, useState } from '#imports'

export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(async (nuxtApp) => {
const hydrationMode = useState<HydrationMode>('nuxt-delay-hydration-mode', () => mode)
if (import.meta.server) {
const event = useRequestEvent()
const routeRules = getRouteRules(event.path)
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)
hydrationMode.value = routeRules.delayHydration
}
Expand Down

0 comments on commit 8e59ef9

Please sign in to comment.