Skip to content

Commit

Permalink
fix(router): make withExtraRoutes function public (#1446)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored Nov 19, 2024
1 parent 1302fb1 commit 2008bd8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
7 changes: 7 additions & 0 deletions apps/analog-app/src/app/about.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Component } from '@angular/core';

@Component({
selector: 'app-about',
template: `About Store`,
})
export default class AboutComponent {}
15 changes: 13 additions & 2 deletions apps/analog-app/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@ import {
provideClientHydration,
withEventReplay,
} from '@angular/platform-browser';
import { provideFileRouter, requestContextInterceptor } from '@analogjs/router';
import {
provideFileRouter,
withExtraRoutes,
requestContextInterceptor,
} from '@analogjs/router';
import { withNavigationErrorHandler } from '@angular/router';

const fallbackRoutes = [
{ path: 'about', loadComponent: () => import('./about') },
];

export const appConfig: ApplicationConfig = {
providers: [
provideFileRouter(withNavigationErrorHandler(console.error)),
provideFileRouter(
withNavigationErrorHandler(console.error),
withExtraRoutes(fallbackRoutes)
),
provideHttpClient(
withFetch(),
withInterceptors([requestContextInterceptor])
Expand Down
22 changes: 22 additions & 0 deletions apps/docs-app/docs/features/routing/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,25 @@ The filesystem-based router will generate the following routes:
| `/products/1` | `products/[productId].page.ts` (layout: `products.page.ts`) |
| `/products/1/edit` | `products/[productId].edit.page.ts` (layout: `products.page.ts`) |
| `/unknown-url` | `[...not-found].md` |

## Providing Extra Routes

Routes can be added manually in addition to the routes discovered through the filesystem. Use the `withExtraRoutes` with an array of routes to be prepended to the discovered routes array. All the routes are merged into a single array.

```ts
import { ApplicationConfig } from '@angular/core';
import { Routes } from '@angular/router';
import { provideFileRouter, withExtraRoutes } from '@analogjs/router';

const customRoutes: Routes = [
{
path: '/custom',
loadComponent: () =>
import('./custom-component').then((m) => m.CustomComponent),
},
];

export const appConfig: ApplicationConfig = {
providers: [provideFileRouter(withExtraRoutes(customRoutes))],
};
```
2 changes: 1 addition & 1 deletion packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
injectRouter,
} from './lib/define-route';
export { RouteMeta } from './lib/models';
export { provideFileRouter } from './lib/provide-file-router';
export { provideFileRouter, withExtraRoutes } from './lib/provide-file-router';
export { MetaTag } from './lib/meta-tags';
export { PageServerLoad, LoadResult } from './lib/route-types';
export { injectLoad } from './lib/inject-load';
Expand Down
24 changes: 16 additions & 8 deletions packages/router/src/lib/provide-file-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ declare const ANALOG_API_PREFIX: string;
export function provideFileRouter(
...features: RouterFeatures[]
): EnvironmentProviders {
const extraRoutesFeature = features.filter((feat) => feat.ɵkind >= 100);
const routerFeatures = features.filter((feat) => feat.ɵkind < 100);

return makeEnvironmentProviders([
provideRouter(routes, ...features),
extraRoutesFeature.map((erf) => erf.ɵproviders),
provideRouter(routes, ...routerFeatures),
{
provide: ENVIRONMENT_INITIALIZER,
multi: true,
Expand All @@ -47,11 +51,15 @@ export function provideFileRouter(
]);
}

export function withExtraRoutes(routes: Routes): RouterFeatures[] {
return [
{
ɵkind: 100 as any,
ɵproviders: [{ provide: ROUTES, useValue: routes }],
},
];
/**
* Provides extra custom routes in addition to the routes
* discovered from the filesystem-based routing. These routes are
* inserted before the filesystem-based routes, and take priority in
* route matching.
*/
export function withExtraRoutes(routes: Routes): RouterFeatures {
return {
ɵkind: 100 as number,
ɵproviders: [{ provide: ROUTES, useValue: routes, multi: true }],
};
}
2 changes: 1 addition & 1 deletion packages/router/src/lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function toRoutes(rawRoutes: RawRoute[], files: Files): Route[] {
path: rawRoute.segment,
loadChildren: () =>
module!().then((m) => {
if (!import.meta.env.PROD) {
if (import.meta.env.DEV) {
const hasModuleDefault = !!m.default;
const hasRedirect = !!m.routeMeta?.redirectTo;

Expand Down

0 comments on commit 2008bd8

Please sign in to comment.