sveltejs / sapper

The next small thing in web development, powered by Svelte
https://sapper.svelte.dev
MIT License
7.01k stars 435 forks source link

Allow specifying entry points programmatically #1737

Closed janosh closed 3 years ago

janosh commented 3 years ago

Is your feature request related to a problem? Please describe.

sapper export --entry / page1 page2 ... doesn't scale if you have dozens of pages that wouldn't be reachable in a production build unless explicitly specified and whose slugs change over time.

Describe the solution you'd like

I'd like to pass in entry points to the sapper export command programmatically to ensure sapper export correctly crawls all pages. Ideally there would be a place in e.g. rollup.config.js where these could be specified:

export default {
  client: {
     // ...
  },

  server: {
    input: config.server.input(),
    output: config.server.output(),
    plugins: [
      entries: await callToMyApiReturningPageSlugs(),
      // ...
    ]
  },
}

Describe alternatives you've considered

Currently we're using an invisible list of page links on the landing page to make sure the crawler finds all pages but this is super clunky:

<script context="module">
  import { fetchPages, fetchPosts } from './queries'

  export async function preload() {
    const pages = await fetchPages()
    const posts = await fetchPosts()
    return { pages, posts }
  }
</script>

<script>
  export let pages, posts
</script>

<!-- placed here so sapper crawls all pages and posts (won't be needed with svelte-kit hopefully) -->
<!-- https://stackoverflow.com/a/63388587 -->
<ul style="visibility: hidden; position: absolute;">
  {#each pages as { title, slug }}
    <a href={slug}>{title}</a>
  {/each}
  {#each posts as { title, slug }}
    <a href={slug}>{title}</a>
  {/each}
</ul>

How important is this feature to you?

I think it would make a great addition to Sapper's API and help users deal with an issue that seems to pop up with Sapper quite frequently which is pages missing from production builds.

Conduitry commented 3 years ago

You can already programmatically construct the command line arguments you pass to sapper export. At this stage in Sapper's lifecycle, we're not going to be adding many new features at all, and definitely not those that already have another pretty simple way to achieve them.

janosh commented 3 years ago

It's a little bit of a hassle. Perhaps you know how to grab console.logs from a node script in such a way that they are quoted and can be fed as a single string to another command? Neither of these seem to work

"export": "sapper export --entry $(node getEntryPoints.mjs)",
"export": "sapper export --entry '$(node getEntryPoints.mjs)'",

The first only enters the first item logged in getEntryPoints.mjs. The second tries to enter

> Crawling http://localhost:3000/$(node/
> Crawling http://localhost:3000/getEntryPoints.mjs)/