Skip to content

Commit

Permalink
[router]: fix _layoutfile with platform extensions registering as a r…
Browse files Browse the repository at this point in the history
…oute
  • Loading branch information
marklawlor committed May 9, 2024
1 parent be289b3 commit 12a3f5a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/expo-router/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### 🐛 Bug fixes

- Fix Typed Routes generating incorrect routes and crashing when moving files ([#28665](https://github.com/expo/expo/pull/28665) by [@marklawlor](https://github.com/marklawlor))
- Fix _layout files with platform extensions incorrectly registering as a route

### 💡 Others

Expand Down
3 changes: 1 addition & 2 deletions packages/expo-router/build/getRoutes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-router/build/getRoutes.js.map

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,45 @@ it(`should only load android and native routes`, () => {
type: 'layout',
});
});

it(`should work with layout routes`, () => {
expect(
getRoutes(
inMemoryContext({
'./(app)/index.tsx': () => null,
'./(app)/_layout.tsx': () => null,
'./(app)/_layout.android.tsx': () => null,
}),
{ internal_stripLoadRoute: true, platform: Platform.OS, skipGenerated: true }
)
).toEqual({
children: [
{
children: [
{
children: [],
contextKey: './(app)/index.tsx',
dynamic: null,
entryPoints: [
'expo-router/build/views/Navigator.js',
'./(app)/_layout.android.tsx',
'./(app)/index.tsx',
],
route: 'index',
type: 'route',
},
],
contextKey: './(app)/_layout.android.tsx',
dynamic: null,
initialRouteName: undefined,
route: '(app)',
type: 'layout',
},
],
contextKey: 'expo-router/build/views/Navigator.js',
dynamic: null,
generated: true,
route: '',
type: 'layout',
});
});
42 changes: 42 additions & 0 deletions packages/expo-router/src/__tests__/platform-routes.test.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,45 @@ it(`should only load android and native routes`, () => {
type: 'layout',
});
});

it(`should work with layout routes`, () => {
expect(
getRoutes(
inMemoryContext({
'./(app)/index.tsx': () => null,
'./(app)/_layout.tsx': () => null,
'./(app)/_layout.ios.tsx': () => null,
}),
{ internal_stripLoadRoute: true, platform: Platform.OS, skipGenerated: true }
)
).toEqual({
children: [
{
children: [
{
children: [],
contextKey: './(app)/index.tsx',
dynamic: null,
entryPoints: [
'expo-router/build/views/Navigator.js',
'./(app)/_layout.ios.tsx',
'./(app)/index.tsx',
],
route: 'index',
type: 'route',
},
],
contextKey: './(app)/_layout.ios.tsx',
dynamic: null,
initialRouteName: undefined,
route: '(app)',
type: 'layout',
},
],
contextKey: 'expo-router/build/views/Navigator.js',
dynamic: null,
generated: true,
route: '',
type: 'layout',
});
});
42 changes: 42 additions & 0 deletions packages/expo-router/src/__tests__/platform-routes.test.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,45 @@ it(`can display platform routes`, () => {
type: 'layout',
});
});

it(`should work with layout routes`, () => {
expect(
getRoutes(
inMemoryContext({
'./(app)/index.tsx': () => null,
'./(app)/_layout.tsx': () => null,
'./(app)/_layout.web.tsx': () => null,
}),
{ internal_stripLoadRoute: true, platform: Platform.OS, skipGenerated: true }
)
).toEqual({
children: [
{
children: [
{
children: [],
contextKey: './(app)/index.tsx',
dynamic: null,
entryPoints: [
'expo-router/build/views/Navigator.js',
'./(app)/_layout.web.tsx',
'./(app)/index.tsx',
],
route: 'index',
type: 'route',
},
],
contextKey: './(app)/_layout.web.tsx',
dynamic: null,
initialRouteName: undefined,
route: '(app)',
type: 'layout',
},
],
contextKey: 'expo-router/build/views/Navigator.js',
dynamic: null,
generated: true,
route: '',
type: 'layout',
});
});
4 changes: 2 additions & 2 deletions packages/expo-router/src/getRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ function getFileMeta(key: string, options: Options) {
const parts = key.split('/');
let route = removeSupportedExtensions(key);
const filename = parts[parts.length - 1];
const filenameWithoutExtensions = removeSupportedExtensions(filename);
const [filenameWithoutExtensions, platformExtension] =
removeSupportedExtensions(filename).split('.');
const isLayout = filenameWithoutExtensions === '_layout';
const isApi = filename.match(/\+api\.(\w+\.)?[jt]sx?$/);

Expand All @@ -352,7 +353,6 @@ function getFileMeta(key: string, options: Options) {
}
let specificity = 0;

const platformExtension = filenameWithoutExtensions.split('.')[1];
const hasPlatformExtension = validPlatforms.has(platformExtension);
const usePlatformRoutes = options.platformRoutes ?? true;

Expand Down

0 comments on commit 12a3f5a

Please sign in to comment.