Skip to content
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

Route-specific app-shell: prerendering dynamic parameterized pages #29425

Open
vzarskus opened this issue Jan 21, 2025 · 2 comments
Open

Route-specific app-shell: prerendering dynamic parameterized pages #29425

vzarskus opened this issue Jan 21, 2025 · 2 comments
Labels
area: @angular/ssr feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature

Comments

@vzarskus
Copy link

Which @angular/* package(s) are relevant/related to the feature request?

platform-server

Description

We have a use case, where we would like to always show the same prerendered page skeleton/carcass on a route that has dynamic parameters.
Looking at the new Angular server documentation, I do not see such option https://angular.dev/guide/hybrid-rendering#parameterized-routes.

Basically we would like posts/:id to return a single prerendered page for any :id parameter.

This page would show a loading skeleton and would let the browser handle the exact :id and the logic related to it.

Proposed solution

Add a wildcard ** route option that would indicate to Angular that this prerendered page handles any dynamic route parameters.

These wildcards could be used as the dynamic parameter in the build time when prerendering happens. It would be the component's responsibility to correctly handle the ** parameter and show some parameter-agnostic content (loaders/skeletons) that would then be prerendered by Angular and served by the server accordingly.

How it could look in app.routes.server.ts:

{
    path: 'posts/**',
    renderMode: RenderMode.Prerender
},

Excerpt from the imaginary post.component.ts:

private subscribeRouter() { // called in ngOnInit
    this.activatedRoute.params
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(params => {
        const id: string = params.id;

        if (id === '**') {
          this.isCarcass = true; // post.component.html will render some skeleton/loader if isCarcass === true
        } else {
          this.getPost(id);
        }
      });
  }

Alternatives considered

I am currently considering:

  1. Adding a "fake" route parameter that I would configure Angular to prerender.
  2. This route would render the skeleton/carcass of the page as per my use case.
  3. Serving the route myself in server.ts with a middleware that runs before any middleware generated by Angular CLI.

app.routes.server.ts:

{
    path: 'posts/:id',
    renderMode: RenderMode.Prerender,
    getPrerenderParams(): Promise<Record<string, string>[]> {
      return Promise.resolve(['carcass'].map(i => ({ id: i })));
    }, // prerenders in browser/posts/carcass
 },

server.ts:

// My new middleware
app.use(
  '/posts/:id',
  express.static(join(browserDistFolder, 'posts', 'carcass', 'index.html'), {
    maxAge: '1y',
    redirect: false,
  }),
);

// Default generated by Angular
app.use(
  express.static(browserDistFolder, {
    maxAge: '1y',
    index: false,
    redirect: false,
  }),
);

// Default generated by Angular
app.get('/**', (req, res, next) => {
  angularApp
    .handle(req)
    .then(response =>
      response ? writeResponseToNodeResponse(response, res) : next(),
    )
    .catch(next);
});

I have tested this approach and it works fine, however, it feels pretty hacky.

@alan-agius4 alan-agius4 transferred this issue from angular/angular Jan 21, 2025
@alan-agius4
Copy link
Collaborator

alan-agius4 commented Jan 21, 2025

It sounds like you're looking to implement an app shell with more granular control, where specific app shells can be assigned to individual routes instead of relying on a single app shell for the entire application.

At the moment, this level of control is not something we support out of the box.

@alan-agius4 alan-agius4 added feature Issue that requests a new feature area: @angular/ssr labels Jan 21, 2025
@alan-agius4 alan-agius4 changed the title Wildcard parameterized routes Angular 19 - how to serve a single prerendered index.html for any parameterized route Wildcard parameterized routes Angular 19 - how to serve a single prerendered index.html (app-shell) for any parameterized route Jan 21, 2025
@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Jan 21, 2025
Copy link
Contributor

angular-robot bot commented Jan 21, 2025

This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

You can find more details about the feature request process in our documentation.

@alan-agius4 alan-agius4 changed the title Wildcard parameterized routes Angular 19 - how to serve a single prerendered index.html (app-shell) for any parameterized route Route-specific app-shells: prerendering dynamic parameterized pages Jan 21, 2025
@alan-agius4 alan-agius4 changed the title Route-specific app-shells: prerendering dynamic parameterized pages Route-specific app-shell: prerendering dynamic parameterized pages Jan 21, 2025
@angular-robot angular-robot bot added feature: under consideration Feature request for which voting has completed and the request is now under consideration and removed feature: votes required Feature request which is currently still in the voting phase labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: @angular/ssr feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

2 participants