ef4 / prember

Prerender Ember apps with Fastboot at build time.
MIT License
195 stars 17 forks source link

Autodiscover simple routes? (without dynamic parts) #40

Open lifeart opened 5 years ago

lifeart commented 5 years ago

Is it possible to use "prember" for simple cases with 0 config?

Routes discovering logic may be taken from https://github.com/wackerservices/SitemapAutogenerator/blob/master/blueprints/sitemap-autogenerator/index.js

ef4 commented 5 years ago

Sure, it’s designed so you can plug in whatever strategy you want. I think that would make a good standalone library.

On Sun, Jul 7, 2019 at 5:24 AM Alex Kanunnikov notifications@github.com wrote:

Is it possible to use "prember" for simple cases with 0 config?

Routes discovering logic may be taken from https://github.com/wackerservices/SitemapAutogenerator/blob/master/blueprints/sitemap-autogenerator/index.js

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ef4/prember/issues/40?email_source=notifications&email_token=AACN6MT3ZRHFFD42R2LMSYDP6GY5TA5CNFSM4H6VHHZKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G5WJ43Q, or mute the thread https://github.com/notifications/unsubscribe-auth/AACN6MTXGBNILGEHDJZ37OTP6GY5TANCNFSM4H6VHHZA .

Wolfr commented 4 years ago

Just discovered this package (very cool btw) and thought I would comment in a relevant issue.

As a newbie to this package, I think: why do I have this supply a list of routes? The router already has a list of routes. Now I need to keep two trees in sync.

It would be awesome if it just took my existing routes (our app is entirely static). Is there some autodiscover function I can use to easily take every existing route?

ef4 commented 4 years ago

It's not the default because it really depends heavily on your use case. Most apps have at least some routes with dynamic segments, which we would either need to ignore (which might be really surprising when people think they're getting all routes prerendered) or error (which would be annoying, because we'd need to offer a configuration system for configuring things explicitly to override the error).

I do think somebody absolutely should ship the strategy you're talking about as a library, and I will link it from the README with an example. It shouldn't take users more than two lines to integrate a URL discovery library with prember.

Wolfr commented 4 years ago

I agree our use case is nonstandard. I also agree that it would be nice if it's a 2 liner config for those who have this use case.

I am very interested in doing things with the Router object in general for my use case. Definitely going to dig into this - also check source code of related projects - and come back later when I have something to contribute.

Wolfr commented 4 years ago

So, I took a good look at this, and just posting my code in case someone has a similar problem.

import Component from '@glimmer/component';
import { inject as service } from '@ember/service';

export default class routeTreeComponent extends Component {
  @service router;

  get routeTree() {
    // This calls an internal, undocumented Ember function to get back an object with route names
    var data = this.router._router._routerMicrolib.recognizer.names;

    // We set up a new variable, and dump the Object keys in there, because it's the only thing we need from this big object
    var routeDataAsArray = []
    routeDataAsArray = Object.keys(data);

    // Now we filter out the parts of the entries of the array that
    // contains the strings loading and error, we don't need those - as well as the application.hbs

    var routeDataAsArray = routeDataAsArray.filter(
      e =>
        !e.includes('error') &&
        !e.includes('loading') &&
        !e.includes('application') &&
        !e.includes('index')
    )

    var routeDataAsArray = routeDataAsArray.map(function(x){return x.replace(/\./g,'/');});

    return routeDataAsArray;
  }

}
{{#each routeTree as |route|}}
  '/{{route}}',<br>
{{/each}}

This code is a component, that when called will output a copy-pastable list that can be pasted into the urls part in ember-cli-build.js.

Now, the next step is to return this array directly in ember-cli-build.js in a smart manner, but my Ember knowledge is not good enough for this.

Note that I am depending on an undocumented API here. So YMMV.

lifeart commented 4 years ago

found this lib - https://github.com/chadhietala/ember-router-mapper