Skip to content

Commit

Permalink
fix: Improve support for older browsers by switching from `replaceAll…
Browse files Browse the repository at this point in the history
…` to `replace` (#885 by @MichalMoravik)

Fixes #884
  • Loading branch information
MichalMoravik authored Mar 15, 2024
1 parent 05ad3d8 commit 080333a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/next-intl/src/middleware/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function formatPathname(template: string, params?: object) {

// Simplify syntax for optional catchall ('[[...slug]]') so
// we can replace the value with simple interpolation
template = template.replaceAll('[[', '[').replaceAll(']]', ']');
template = template.replace(/\[\[/g, '[').replace(/\]\]/g, ']');

let result = template;
Object.entries(params).forEach(([key, value]) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/next-intl/src/shared/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ export function matchesPathname(
export function templateToRegex(template: string): RegExp {
const regexPattern = template
// Replace optional catchall ('[[...slug]]')
.replaceAll(/\[\[(\.\.\.[^\]]+)\]\]/g, '?(.*)')
.replace(/\[\[(\.\.\.[^\]]+)\]\]/g, '?(.*)')
// Replace catchall ('[...slug]')
.replaceAll(/\[(\.\.\.[^\]]+)\]/g, '(.+)')
.replace(/\[(\.\.\.[^\]]+)\]/g, '(.+)')
// Replace regular parameter ('[slug]')
.replaceAll(/\[([^\]]+)\]/g, '([^/]+)');
.replace(/\[([^\]]+)\]/g, '([^/]+)');

return new RegExp(`^${regexPattern}$`);
}

0 comments on commit 080333a

Please sign in to comment.