Skip to content

Fix preloading in standalone applications with nested routes #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion projects/ngx-quicklink/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"name": "ngx-quicklink",
"homepage": "https://github.com/mgechev/ngx-quicklink",
"bugs": "https://github.com/mgechev/ngx-quicklink/issues",
"repository": {
"type": "git",
"url": "https://github.com/mgechev/ngx-quicklink.git"
},
"version": "0.4.1",
"peerDependencies": {
"@angular/common": "^15.0.0",
Expand All @@ -8,4 +14,4 @@
"dependencies": {
"tslib": "^2.3.0"
}
}
}
12 changes: 7 additions & 5 deletions projects/ngx-quicklink/src/lib/quicklink-strategy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ const findPath = (config: Route[], route: Route): string => {
if (route && route.children) {
children = children.concat(route.children);
}
children.forEach((r: Route) => {
if (visited.has(r)) return;
parent.set(r, el);
config.push(r);
});
}

children.forEach((r) => {
if (visited.has(r)) return;
parent.set(r, el);
config.push(r);
});
}

let path = '';
let current: Route | undefined = route;

Expand Down
7 changes: 7 additions & 0 deletions projects/standalone-app/src/app/about.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';
import { QuicklinkDirective } from 'ngx-quicklink';

@Component({
standalone: true,
imports: [RouterModule, QuicklinkDirective],
template: `
About component

<a [routerLink]="['/', 'about', 'sub']">SUB</a>

<router-outlet></router-outlet>
`,
})
export default class AboutComponent {}
23 changes: 23 additions & 0 deletions projects/standalone-app/src/app/app.component.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
:host {
display: block;
padding: 24px;
}

.navigation {
display: flex;
flex-direction: row;
gap: 12px;
margin-bottom: 24px;

a {
color: black;
text-decoration: none;
}
}

.side {
position: absolute;
right: 0;
width: 20vw;
border: solid 1px;
}
36 changes: 34 additions & 2 deletions projects/standalone-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';
import { QuicklinkDirective } from 'ngx-quicklink';

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterModule],
styleUrls: ['./app.component.less'],
imports: [RouterModule, QuicklinkDirective],
template: `
<h1>Standalone Example</h1>

<div class="navigation">
<a routerLink="['/']">Root</a>
<a [routerLink]="primaryRoutePath">Home</a>
<a [routerLink]="['/', 'about']">About</a>
<a [routerLink]="secondaryRoutePath">Social</a>
</div>

<router-outlet></router-outlet>

<div class="side">
Side Outlet
<router-outlet name="side"></router-outlet>
</div>
`,
})
export class AppComponent {}
export class AppComponent {
primaryRoutePath: any[] = [];
secondaryRoutePath: any[] = [];

constructor() {
// The timeouts are used to demonstrate dynamically preloading routes
// Check the network tab to see this in action!
setTimeout(() => {
this.primaryRoutePath = ['/', 'home'];
console.log('Home route added');
}, 3000);
setTimeout(() => {
this.secondaryRoutePath = ['/', { outlets: { side: ['social'] } }];
console.log('Social route added');
}, 2000);
}
}
16 changes: 2 additions & 14 deletions projects/standalone-app/src/app/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ import { QuicklinkDirective } from 'ngx-quicklink';
@Component({
standalone: true,
imports: [RouterLink, QuicklinkDirective],
template: `
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<a routerLink="/about">About</a>
`,
template: ` Home Component `,
})
export default class HomeComponent {
}
export default class HomeComponent {}
10 changes: 10 additions & 0 deletions projects/standalone-app/src/app/root.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { QuicklinkDirective } from 'ngx-quicklink';

@Component({
standalone: true,
imports: [RouterLink, QuicklinkDirective],
template: ` Root Component `,
})
export default class RootComponent {}
10 changes: 10 additions & 0 deletions projects/standalone-app/src/app/social.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { QuicklinkDirective } from 'ngx-quicklink';

@Component({
standalone: true,
imports: [RouterLink, QuicklinkDirective],
template: ` Social Component `,
})
export default class SocialComponent {}
8 changes: 8 additions & 0 deletions projects/standalone-app/src/app/subfolder/subcomponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component } from '@angular/core';

@Component({
standalone: true,
imports: [],
template: ` SUB COMPONENT `,
})
export default class SubComponent {}
22 changes: 21 additions & 1 deletion projects/standalone-app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,32 @@ bootstrapApplication(AppComponent, {
{
path: 'about',
loadComponent: () => import('./app/about.component'),
children: [
{
path: 'sub',
loadComponent: () => import('./app/subfolder/subcomponent'),
},
],
},
{
path: '',
path: 'home',
pathMatch: 'full',
loadComponent: () => import('./app/home.component'),
},
{
path: 'social',
loadComponent: () => import('./app/social.component'),
outlet: 'side',
},
{
path: 'root',
pathMatch: 'full',
loadComponent: () => import('./app/root.component'),
},
{
path: '**',
redirectTo: 'root',
},
],
withPreloading(QuicklinkStrategy)
),
Expand Down
4 changes: 4 additions & 0 deletions projects/standalone-app/src/styles.css
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/* You can add global styles to this file, and also import other style files */
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
4 changes: 4 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/* You can add global styles to this file, and also import other style files */
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}