Skip to content

The only tool you need to know what is happening and how to fix it.

bug: slight updates to handle the urls properly

dduxd3c46cb3a4b7
diff --git a/docs/components/SdkContext.jsx b/docs/components/SdkContext.jsx
index faddf14c..ff4b706d 100644
--- a/docs/components/SdkContext.jsx
+++ b/docs/components/SdkContext.jsx
@@ -13,6 +13,14 @@ export const SDK_OPTIONS = [
const STORAGE_KEY = 'traceway-docs-sdk'
const VALID_VALUES = new Set(SDK_OPTIONS.map((o) => o.value))
+const PATH_SDK_MAP = {
+ 'gin-middleware': 'go-gin',
+ 'chi-middleware': 'go-chi',
+ 'fiber-middleware': 'go-fiber',
+ 'fasthttp-middleware': 'go-fasthttp',
+ 'http-middleware': 'go-http',
+}
+
const SdkContext = createContext({
sdk: 'go-gin',
setSdk: () => {},
@@ -51,14 +59,41 @@ export function SdkProvider({ children }) {
function handleRouteChange(url) {
if (!url.startsWith('/client')) return
- const [pathname, search] = url.split('?')
- const params = new URLSearchParams(search || '')
+ const hashIdx = url.indexOf('#')
+ const questionIdx = url.indexOf('?')
+
+ let pathname, search, hash
+ if (hashIdx === -1 && questionIdx === -1) {
+ pathname = url; search = ''; hash = ''
+ } else if (questionIdx !== -1 && (hashIdx === -1 || questionIdx < hashIdx)) {
+ pathname = url.substring(0, questionIdx)
+ const rest = url.substring(questionIdx + 1)
+ const restHash = rest.indexOf('#')
+ if (restHash !== -1) {
+ search = rest.substring(0, restHash)
+ hash = rest.substring(restHash)
+ } else {
+ search = rest; hash = ''
+ }
+ } else {
+ pathname = url.substring(0, hashIdx)
+ const rest = url.substring(hashIdx)
+ const restQ = rest.indexOf('?')
+ if (restQ !== -1) {
+ hash = rest.substring(0, restQ)
+ search = rest.substring(restQ + 1)
+ } else {
+ hash = rest; search = ''
+ }
+ }
+
+ const params = new URLSearchParams(search)
if (pathname === '/client') {
if (params.has('sdk')) {
params.delete('sdk')
const qs = params.toString()
- router.replace(pathname + (qs ? '?' + qs : ''), undefined, { shallow: true })
+ router.replace(pathname + (qs ? '?' + qs : '') + hash, undefined, { shallow: true })
}
localStorage.removeItem(STORAGE_KEY)
setSdkState('go-gin')
@@ -66,10 +101,22 @@ export function SdkProvider({ children }) {
}
if (!params.get('sdk')) {
- const currentSdk = localStorage.getItem(STORAGE_KEY) || sdk
- if (currentSdk && VALID_VALUES.has(currentSdk)) {
- params.set('sdk', currentSdk)
- router.replace(pathname + '?' + params.toString(), undefined, { shallow: true })
+ let detectedSdk = null
+ for (const [folder, sdkValue] of Object.entries(PATH_SDK_MAP)) {
+ if (pathname.includes('/' + folder)) {
+ detectedSdk = sdkValue
+ break
+ }
+ }
+ const sdkToUse = detectedSdk || localStorage.getItem(STORAGE_KEY) || sdk
+ if (sdkToUse && VALID_VALUES.has(sdkToUse)) {
+ params.set('sdk', sdkToUse)
+ if (detectedSdk) {
+ setSdkState(detectedSdk)
+ localStorage.setItem(STORAGE_KEY, detectedSdk)
+ }
+ const qs = params.toString()
+ router.replace(pathname + (qs ? '?' + qs : '') + hash, undefined, { shallow: true })
}
}
}