Skip to content

Commit

Permalink
fix: resolve filter properly
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 25, 2022
1 parent 8231ec0 commit c2e18c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
39 changes: 38 additions & 1 deletion packages/nuxt-delay-hydration/src/runtime/nitro-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import type { NitroAppPlugin } from 'nitropack'
import { packString } from 'packrup'
import { MODE_DELAY_APP_INIT } from '../module'
import { createFilter } from '../util/filter'
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)[]
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 <NitroAppPlugin> function (nitro) {
nitro.hooks.hook('render:html', (htmlContext, { event }) => {
if (include.length || exclude.length) {
Expand Down
37 changes: 0 additions & 37 deletions packages/nuxt-delay-hydration/src/util/filter.ts

This file was deleted.

0 comments on commit c2e18c3

Please sign in to comment.