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

Allow multiple pages in the same bundle for development performance improvement #4843

Closed
vjpr opened this issue Jul 25, 2018 · 1 comment
Closed

Comments

@vjpr
Copy link

vjpr commented Jul 25, 2018

Feature request

As an alternative to #4841 (compiling multiple entry points at once).

Allow bundling multiple pages into the same entry point, like a traditional webpack app.

Then modifying the router to check if the page is loaded, and using it.

Problem

Link navigation is slow during dev due to webpack recompilation.

When visiting a page, a bundle is served for each page. When you click a link, it triggers a webpack build of the next page's bundle (twice server/client), then SSRs it, then serves the js file to the client, its evaled and run client-side, and you see the next page.

Solution

So to prevent any server roundtrips, serve multiple pages in a single bundle.

CON: This will make the initial build slightly slower.

PRO: Each page can share common deps. Link visits are instant.

Impl

async getRouteInfo (route, pathname, query, as) {
    let routeInfo = null

    try {
      routeInfo = this.components[route]

	  // ADD THIS
      if (window.__routePages && window.__routePages[route]) {
        routeInfo = {Component: window.__routePages[route]}
      }
      // ---

      if (!routeInfo) {
        routeInfo = { Component: await this.fetchComponent(route, as) }
      }

      const { Component } = routeInfo

Then set window.__routePages to the pages you want to load in the bundle. This could be in _app.js for example.

This can already be achieved in user code.

Make sure to turn off this during prod, otherwise SSR won't work.

Are there any other caveats that I am missing?

Alternative

Use something like https://github.com/zachrbrown/next-react-router. Basically, you are embedding a non-SSR SPA in your next app.

One approach is to extract your pages into separate modules, and your pages/ files will just re-export these modules. Then you can have all the advantages of speedy development, and also SSR in production.


All these combined make Next quite compelling for SSR and even if your don't need SSR.

Next.js's custom-built on-demand-entries plugin makes developing very large sites much faster in terms of initial startup and incremental builds. The downside is the slowness in compiling a separate entrypoint for each page. But by bundling groups of pages together you get the best of both worlds.

@timneutkens
Copy link
Member

Tracking optimizations in #4844

@lock lock bot locked as resolved and limited conversation to collaborators Jan 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants