Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 404 URLs on path-based i18n projects
- Loading branch information
Showing
8 changed files
with
135 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
'skeleton': patch | ||
'@shopify/cli-hydrogen': patch | ||
--- | ||
|
||
This is an important fix to a bug with 404 routes and path-based i18n projects where some unknown routes would not properly render a 404. This fixes all new projects, but to fix existing projects, add a `($locale).tsx` route with the following contents: | ||
|
||
```ts | ||
import {type LoaderFunctionArgs} from '@remix-run/server-runtime'; | ||
|
||
export async function loader({params, context}: LoaderFunctionArgs) { | ||
const {language, country} = context.storefront.i18n; | ||
|
||
if ( | ||
params.locale && | ||
params.locale.toLowerCase() !== `${language}-${country}`.toLowerCase() | ||
) { | ||
// If the locale URL param is defined, yet we still are still at the default locale | ||
// then the the locale param must be invalid, send to the 404 page | ||
throw new Response(null, {status: 404}); | ||
} | ||
|
||
return null; | ||
} | ||
``` |
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
16 changes: 16 additions & 0 deletions
16
packages/cli/src/lib/setups/routes/templates/($locale).tsx
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,16 @@ | ||
import {type LoaderFunctionArgs} from '@remix-run/server-runtime'; | ||
|
||
export async function loader({params, context}: LoaderFunctionArgs) { | ||
const {language, country} = context.storefront.i18n; | ||
|
||
if ( | ||
params.locale && | ||
params.locale.toLowerCase() !== `${language}-${country}`.toLowerCase() | ||
) { | ||
// If the locale URL param is defined, yet we still are still at the default locale | ||
// then the the locale param must be invalid, send to the 404 page | ||
throw new Response(null, {status: 404}); | ||
} | ||
|
||
return null; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {type LoaderFunctionArgs} from '@remix-run/server-runtime'; | ||
|
||
export async function loader({params, context}: LoaderFunctionArgs) { | ||
const {language, country} = context.storefront.i18n; | ||
|
||
if ( | ||
params.locale && | ||
params.locale.toLowerCase() !== `${language}-${country}`.toLowerCase() | ||
) { | ||
// If the locale URL param is defined, yet we still are still at the default locale | ||
// then the the locale param must be invalid, send to the 404 page | ||
throw new Response(null, {status: 404}); | ||
} | ||
|
||
return null; | ||
} |