Skip to content

Commit fdc96ac

Browse files
committedFeb 20, 2025·
refactor: optimized url, createUrl and goto
1 parent e9e55e9 commit fdc96ac

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed
 

‎lib/runtime/Route/Route.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createUrl } from '../helpers/index.js'
1+
import { getCreateUrl } from '../helpers/index.js'
22
import { populateUrl } from '../utils/index.js'
33
import { RouteFragment } from './RouteFragment.js'
44
import { LoadCache } from './utils.js'
@@ -138,7 +138,7 @@ export class Route {
138138
/** @type { RoutifyLoadContext } */
139139
const ctx = {
140140
route: this,
141-
url: createUrl(fragment, this.router),
141+
url: getCreateUrl(fragment, this.router),
142142
prevRoute,
143143
isNew: !isSameBranch,
144144
fetch,

‎lib/runtime/helpers/index.js

+11-27
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,10 @@ export const goto = {
7676
return derived(url, $url =>
7777
/** @type {Goto} */
7878
(pathOrNode, userParams, options) => {
79-
const path =
80-
typeof pathOrNode === 'string' ? pathOrNode : pathOrNode?.path
81-
8279
/** @type {options} */
8380
const defaults = { mode: 'push', state: {} }
8481
options = { ...defaults, ...options }
85-
const newUrl = $url(path, userParams, options)
82+
const newUrl = $url(pathOrNode, userParams, options)
8683
router.url[options.mode](newUrl, options.state)
8784
return ''
8885
},
@@ -91,7 +88,7 @@ export const goto = {
9188
}
9289

9390
/**
94-
* @typedef {(<T extends string | HTMLAnchorElement>(
91+
* @typedef {(<T extends string | RNodeRuntime | HTMLAnchorElement>(
9592
* inputPath: T,
9693
* userParams?: { [x: string]: string; },
9794
* options?: Partial<$UrlOptions>
@@ -105,29 +102,16 @@ export const url = {
105102
subscribe: (run, invalidate) => {
106103
const { fragment, router } = contexts
107104

108-
let InitialElem, initialParams, initialPath
109-
110-
const createUrlString = createUrl(fragment.fragment, router)
111-
const getInitialUrl = () => createUrlString(initialPath, ...initialParams)
112-
113105
return derived(fragment.params, $params => {
114-
if (InitialElem) {
115-
// if we're dealing with an already set anchor element, set the href
116-
InitialElem.setAttribute('href', getInitialUrl())
117-
}
118-
119-
// todo for some reason we always need to return this function, but it really shouldn't be necessary when an elem has been set
120-
return (pathOrElem, ...params) => {
106+
return (pathElemOrNode, params, options) => {
107+
const createUrl = getCreateUrl(fragment.fragment, router)
121108
// if we're dealing with a string, return the rendered url
122-
if (typeof pathOrElem != 'object') {
123-
return createUrlString(pathOrElem, ...params)
124-
}
125-
126-
// if we're dealing with an anchor element, store it
127-
InitialElem = pathOrElem
128-
initialParams = params
129-
initialPath = InitialElem.getAttribute('href')
130-
InitialElem.setAttribute('href', getInitialUrl())
109+
if (!(pathElemOrNode instanceof HTMLAnchorElement))
110+
return createUrl(pathElemOrNode, params, options)
111+
112+
// if we're dealing with an anchor element, update it
113+
const path = pathElemOrNode.getAttribute('href')
114+
pathElemOrNode.setAttribute('href', createUrl(path, $params, options))
131115
}
132116
}).subscribe(run, invalidate)
133117
},
@@ -146,7 +130,7 @@ export const url = {
146130
* @param {Router} router
147131
* @returns {UrlFromString}
148132
*/
149-
export const createUrl =
133+
export const getCreateUrl =
150134
(fragment, router) =>
151135
/** @type {UrlFromString} */
152136
(pathOrNode, userParams = {}, options = {}) => {

0 commit comments

Comments
 (0)
Please sign in to comment.