Skip to content
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

Fix server-side function extractQueryParamsAndHash #164

Merged
merged 1 commit into from
Sep 27, 2024
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
9 changes: 7 additions & 2 deletions examples/example-ssr/src/pages/BarPage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useRef } from "react"
import { Link, useStack } from "@cher-ami/router"
import { Link, useRouter, useStack } from "@cher-ami/router"
import { transitionsHelper } from "~/helpers/transitionsHelper"
import { EPages } from "~/routes"

const componentName = "BarPage"
function BarPage(props, handleRef) {
const rootRef = useRef(null)
const { currentRoute } = useRouter()

useStack({
componentName,
Expand All @@ -17,7 +18,11 @@ function BarPage(props, handleRef) {

return (
<div className={[componentName].filter((e) => e).join(" ")} ref={rootRef}>
{componentName}
<h1>{componentName}</h1>
Query Params :
<ul>
<li>Hello : {currentRoute.queryParams?.hello} </li>
</ul>
<br />
<br />
<Link to={{ name: EPages.FOO }}>link to FOO</Link>
Expand Down
44 changes: 29 additions & 15 deletions src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,20 @@ export function getNotFoundRoute(routes: TRoute[]): TRoute {
)
}

/**
* Extract hash,path and query params from full url
* Ex :
* url "/a/b#abc?foo=bar"
* returns
* {
* hash : "abc"
* urlWithoutHashAndQuery : "/a/b"
* queryParams: {foo: "bar"}
* }
* @param url
* @param isHashHistory
* @returns {} Query params, hash & path
*/
export const extractQueryParamsAndHash = (
url: string,
isHashHistory?: boolean,
Expand All @@ -392,24 +406,14 @@ export const extractQueryParamsAndHash = (
hash: string
urlWithoutHashAndQuery: string
} => {
const parseQueryParams = (queryString) => {
const params = {}
const queryPairs = queryString.split("&")
queryPairs.forEach((pair) => {
const [key, value] = pair.split("=")
if (key) params[decodeURIComponent(key)] = decodeURIComponent(value || "")
})
return params
}

let path = ""
let queryParams = {}
let hash = null
let newUrl = url

if (isHashHistory) {
// For hash history, we extract the part after the `#/`
const [rawPath, rawQueryParams] = url.split("?")
newUrl = rawPath || "/" // Default to `/` if no path
path = rawPath || "/" // Default to `/` if no path
queryParams = parseQueryParams(rawQueryParams || "")
} else {
// For non-hash history, we handle the path, query params, and hash separately
Expand All @@ -418,11 +422,11 @@ export const extractQueryParamsAndHash = (
const queryIndex = baseUrl.indexOf("?")

if (queryIndex !== -1) {
newUrl = baseUrl.substring(0, queryIndex).replace(window.location.origin, "") // Extract path
path = baseUrl.substring(0, queryIndex) // Extract path
const rawQueryParams = baseUrl.substring(queryIndex + 1) // Get query string
queryParams = parseQueryParams(rawQueryParams) // Parse query string into object
} else {
newUrl = baseUrl.replace(window.location.origin, "") // Only the path if no query params
path = baseUrl // Only the path if no query params
}

// Now extract the hash part (everything after the `#`)
Expand All @@ -431,7 +435,17 @@ export const extractQueryParamsAndHash = (
}
}

return { queryParams, hash, urlWithoutHashAndQuery: newUrl }
return { queryParams, hash, urlWithoutHashAndQuery: path }
}

const parseQueryParams = (queryString) => {
const params = {}
const queryPairs = queryString.split("&")
queryPairs.forEach((pair) => {
const [key, value] = pair.split("=")
if (key) params[decodeURIComponent(key)] = decodeURIComponent(value || "")
})
return params
}

// ----------------------------------------------------------------------------- ROUTES
Expand Down
Loading