-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useUrl #770
Comments
How about making it reactive in the client (on history change event)? |
awesome idea! |
But in a composable way, so keep useUrl and add createUrl. |
function useUrl() {
const [currentUrl, setCurrentUrl] = createSignal(
new URL(isServer ? getRequestEvent()!.request.url : location.href)
);
createEffect(() => {
const handleLocationChange = () => {
setCurrentUrl(new URL(location.href));
};
addEventListener("popstate", handleLocationChange);
addEventListener("hashchange", handleLocationChange);
return () => {
removeEventListener("popstate", handleLocationChange);
removeEventListener("hashchange", handleLocationChange);
};
});
return currentUrl;
} updated code with help from claude. you can add anything else you want. |
Claude still treats Solid as if it was React. Instead of returning the unsubscriber, you need to wrap it in onCleanup. |
createEffect(() => {
const handleLocationChange = () => {
setCurrentUrl(new URL(location.href));
};
addEventListener("popstate", handleLocationChange);
addEventListener("hashchange", handleLocationChange);
onCleanup(() => {
removeEventListener("popstate", handleLocationChange);
removeEventListener("hashchange", handleLocationChange);
});
}); is this correct? |
we don't need an effect here, should i just change it to an |
Since there are no reactive properties inside the effect, it is actually the same, but onMount is shorter, so go for it. |
ok can you create |
createHydratableSignal is only meant for signals that are supposed to have different initial values on the server and on the client during hydration. Since our signal should basically have the same initial value, it is not necessary here. In addition, we should use a equals function in the signal to avoid triggering updates when the URL itself is the same, only wrapped in a new object. function useUrl() {
return new URL(isServer ? getRequestEvent()!.request.url : location.href);
}
function createUrl() {
const [currentUrl, setCurrentUrl] = createSignal(
useUrl(),
{ equals: (a, b) => a.toString() === b.toString() }
);
onMount(() => {
const handleLocationChange = () => {
setCurrentUrl(new URL(location.href));
};
addEventListener("popstate", handleLocationChange);
addEventListener("hashchange", handleLocationChange);
onCleanup(() => {
removeEventListener("popstate", handleLocationChange);
removeEventListener("hashchange", handleLocationChange);
});
});
return currentUrl;
} |
sounds awesome! is that all? |
small detail: should we call it createUrl/useUrl or createURL/useURL 🤔 |
Describe The Problem To Be Solved
on the server
getRequestEvent()
hasRequest.url
but its not a convenientURL
object, and on the client we havelocation
which is not exactly the same asURL
Suggest A Solution
should this be added? just a suggestion!
The text was updated successfully, but these errors were encountered: