Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion signal-router/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"preact": "npm:preact@^10.27.2"
},
"name": "@01edu/signal-router",
"version": "0.1.6",
"version": "0.1.7",
"license": "MIT",
"exports": { ".": "./mod.tsx" },
"compilerOptions": {
Expand Down
23 changes: 22 additions & 1 deletion signal-router/mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const isCurrentURL = (alt: URL) => {
return true
}

const baseURL = new URL(document.baseURI)

// ensure we never have trailing /
const initialUrl = new URL(location.href)
if (location.pathname.at(-1) === '/') {
Expand Down Expand Up @@ -91,7 +93,7 @@ type GetUrlProps = {

const getUrl = ({ href, hash, params }: GetUrlProps): URL => {
const currentUrl = urlSignal.value
const url = new URL(href || currentUrl, origin)
const url = new URL(href || currentUrl, baseURL.href)
hash != null && (url.hash = hash)
url.pathname.at(-1) === '/' && (url.pathname = url.pathname.slice(0, -1))
if (!params) {
Expand Down Expand Up @@ -284,6 +286,15 @@ const params = new Proxy({} as Record<string, Signal<string | null>>, {
// url.params: { id: 454, option: 'open' }
const hashSignal = computed(() => urlSignal.value.hash)
const pathSignal = computed(() => urlSignal.value.pathname)
const matchPath = (path: string) =>
`${baseURL.pathname}${path}` === `${pathSignal.value}`

const relativePath = computed(() => {
const path = pathSignal.value
const base = baseURL.pathname.replace(/\/$/, '')
if (!path.startsWith(base)) return path
return path.slice(base.length) || '/'
})
/**
* Reactive URL helpers.
*
Expand All @@ -308,7 +319,17 @@ export const url: {
peek: () => URL
params: Record<string, string | null>
equals: (url: URL) => boolean
base: URL
relativePath: string
matchPath: (...paths: string[]) => boolean
} = {
base: baseURL,
get relativePath() {
return relativePath.value
},
matchPath(...paths: string[]) {
return paths.some(matchPath)
},
get path() {
return pathSignal.value
},
Expand Down