forked from vikejs/vike
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
export default onBeforeRoute | ||
|
||
import { extractLocale } from '../locales' | ||
import { modifyUrl } from 'vike/modifyUrl' | ||
|
||
function onBeforeRoute(pageContext) { | ||
const { urlWithoutLocale, locale } = extractLocale(pageContext.urlPathname) | ||
const url = pageContext.urlParsed | ||
const { urlPathnameWithoutLocale, locale } = extractLocale(url.pathname) | ||
const urlLogical = modifyUrl(url.href, { pathname: urlPathnameWithoutLocale }) | ||
return { | ||
pageContext: { | ||
// Make `locale` available as pageContext.locale | ||
locale, | ||
// Vike's router will use pageContext.urlLogical instead of pageContext.urlOriginal | ||
urlLogical: urlWithoutLocale | ||
urlLogical | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export { modifyUrl } | ||
|
||
import { createUrlFromComponents, parseUrl } from './utils.js' | ||
|
||
/** | ||
* Modify a URL. | ||
* | ||
* Example: changing the URL pathname for internationalization. | ||
* | ||
* https://vike.dev/modifyUrl | ||
*/ | ||
function modifyUrl( | ||
url: string, | ||
modify: { | ||
pathname?: string | ||
hostname?: string | ||
port?: number | ||
protocol?: string | ||
} | ||
): string { | ||
const urlParsed = parseUrl(url, '/') | ||
|
||
// Pathname | ||
const pathname = modify.pathname ?? urlParsed.pathname | ||
|
||
// Origin | ||
const originParts: string[] = [ | ||
modify.protocol ?? urlParsed.protocol ?? '', | ||
modify.hostname ?? urlParsed.hostname ?? '' | ||
] | ||
const port = modify.port ?? urlParsed.port | ||
if (port || port === 0) { | ||
originParts.push(`:${port}`) | ||
} | ||
const origin = originParts.join('') | ||
|
||
const urlModified = createUrlFromComponents( | ||
origin, | ||
pathname, | ||
// Should we also support modifying search and hash? | ||
urlParsed.searchOriginal, | ||
urlParsed.hashOriginal | ||
) | ||
return urlModified | ||
} |