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

Autodiscover simple routes? (without dynamic parts) #40

Open
lifeart opened this issue Jul 7, 2019 · 6 comments
Open

Autodiscover simple routes? (without dynamic parts) #40

lifeart opened this issue Jul 7, 2019 · 6 comments

Comments

@lifeart
Copy link

lifeart commented Jul 7, 2019

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
Copy link
Owner

ef4 commented Jul 7, 2019 via email

@Wolfr
Copy link

Wolfr commented Jun 11, 2020

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
Copy link
Owner

ef4 commented Jun 11, 2020

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
Copy link

Wolfr commented Jun 11, 2020

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
Copy link

Wolfr commented Jun 17, 2020

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
Copy link
Author

lifeart commented Jun 22, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants