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 (#1732)
* Fix 404 URLs on path-based i18n projects --------- Co-authored-by: Fran Dios <fran.dios@shopify.com>
- Loading branch information
Showing
11 changed files
with
187 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// <reference types="@shopify/remix-oxygen" /> | ||
|
||
import type { | ||
storefront, | ||
customeraccount, | ||
hydrogencart, | ||
} from '@shopify/hydrogen'; | ||
|
||
declare module '@shopify/remix-oxygen' { | ||
/** | ||
* Declare local additions to the Remix loader context. | ||
*/ | ||
export interface AppLoadContext { | ||
env: Env; | ||
cart: HydrogenCart; | ||
storefront: Storefront; | ||
customerAccount: CustomerAccount; | ||
waitUntil: ExecutionContext['waitUntil']; | ||
} | ||
} |
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-check.ts
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 '@shopify/remix-oxygen'; | ||
|
||
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
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 '@shopify/remix-oxygen'; | ||
|
||
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; | ||
} |