Skip to content

Commit

Permalink
fix: mount filter bug
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 25, 2022
1 parent 1f77198 commit 97004d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
38 changes: 37 additions & 1 deletion packages/nuxt-delay-hydration/src/runtime/mount-plugin.client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
import { createFilter } from './filter'
import { createRouter, toRouteMatcher } from 'radix3'
import { defineNuxtPlugin } from '#app'
import { exclude, include } from '#delay-hydration'

export interface CreateFilterOptions {
include?: (string | RegExp)[]
exclude?: (string | RegExp)[]
strictTrailingSlash?: boolean
}

export function createFilter(options: CreateFilterOptions = {}): (path: string) => boolean {
const include = options.include || []
const exclude = options.exclude || []

return function (path: string): boolean {
for (const v of [{ rules: exclude, result: false }, { rules: include, result: true }]) {
const regexRules = v.rules.filter(r => r instanceof RegExp) as RegExp[]
if (regexRules.some(r => r.test(path)))
return v.result

const stringRules = v.rules.filter(r => typeof r === 'string') as string[]
if (stringRules.length > 0) {
const routes = {}
for (const r of stringRules) {
// quick scan of literal string matches
if (r === path)
return v.result

// need to flip the array data for radix3 format, true value is arbitrary
routes[r] = true
}
const routeRulesMatcher = toRouteMatcher(createRouter({ routes, ...options }))
if (routeRulesMatcher.matchAll(path).length > 0)
return Boolean(v.result)
}
}
return include.length === 0
}
}

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hooks.hook('app:created', async () => {
// only if we're ssr
Expand Down
5 changes: 1 addition & 4 deletions packages/nuxt-delay-hydration/src/runtime/nitro-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { NitroAppPlugin } from 'nitropack'
import { packString } from 'packrup'
import { MODE_DELAY_APP_INIT } from '../module'
import { createRouter, toRouteMatcher } from 'radix3'
import { useRuntimeConfig } from '#imports'

import { debug, exclude, include, mode, replayScript, script } from '#delay-hydration'

const SCRIPT_REGEX = /<script(.*?)>/gm


export interface CreateFilterOptions {
include?: (string | RegExp)[]
exclude?: (string | RegExp)[]
Expand Down Expand Up @@ -54,7 +51,7 @@ export default <NitroAppPlugin> function (nitro) {
return
}

const isDelayingInit = mode === MODE_DELAY_APP_INIT
const isDelayingInit = mode === 'init'
let extraScripts = ''
let isPageSSR = true
if (isDelayingInit) {
Expand Down

0 comments on commit 97004d4

Please sign in to comment.