brillout / goldpage

Page Builder.
Creative Commons Zero v1.0 Universal
57 stars 3 forks source link

Infer route in page config from path when not provided? #12

Closed chriscalo closed 4 years ago

chriscalo commented 4 years ago

If the config object returned from a *.page.js file doesn’t include a route field, can it be derived from its relative path in the pages directory?

For example:

pages/index.page.js => /
pages/foo.page.js => /foo
pages/bar/index.page.js => /bar/

I created a small utility to do this. Here’s what its usage looks like:

import page from "./index.vue";
import { fileroute } from "~/util/fileroute";

export default {
  route: fileroute(__filename),
  view: page,
}

And here’s the fileroute function:

import path from "path";

// convert *.page.js file path into route string
export function fileroute(filename) {
  const route = String(filename)
    // web server routes are relative to pages dir
    .replace("pages/", "/")
    // use parent folder name for index pages
    .replace("index.page.js", "/")
    // use page name
    .replace(".page.js", "")
    // fix double slashes
    .replace("//", "/");

  return route;
};

Goldpage is already transforming the paths of all *.page.js files into names like index or foo or bar/index, could the name be transformed into a route if a route isn't present on a page config object?

brillout commented 4 years ago

I have a preference over having only one way of doing things. If Goldpage wouldn't support parameterized rotues then yes I'd agree with you and the one way of doing things would be to use the name of .page.js files and paths to determine the route.

Goldpage does support parameterized routes such as hello/:name which means that there could be filenames like pages/hello\/:name/index.page.js. For such a route I'd prefer having a route page config instead.

If you have 20 pages then you'll have to write 20 route strings. It's redundant I agree but I'd say worth it for the sake of simplicity.

Allowing only one way of doing things makes things simpler.

chriscalo commented 4 years ago

Very reasonable position. Thanks for giving it some thought 👍