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

[v2] improve how we load GraphQL query results in development/production (aka Ludicrous Mode) #4555

Merged
merged 38 commits into from
Apr 6, 2018

Conversation

m-allanson
Copy link
Contributor

@m-allanson m-allanson commented Mar 16, 2018

The beginnings of a custom JSON loader for query results. In develop mode this pushes query results straight to the browser via websockets:

kapture 2018-03-15 at 21 10 55

@ghost ghost assigned m-allanson Mar 16, 2018
@ghost ghost added the review label Mar 16, 2018
@KyleAMathews KyleAMathews changed the title [WIP] Custom JSON loader [v2][WIP] Custom JSON loader Mar 16, 2018
@pieh
Copy link
Contributor

pieh commented Mar 16, 2018

Here's dirty WIP for build based on this PR json-loader...pieh:json-loader (I reverted change to write json directly to public):

My current TO-DO:

Results:
Using https://github.com/clocklimited/GatsbyScaleTest updated to use gatsby v2 and generating 2000 pages locally (I've run test 5 times for each, so not great sample size - will run more thorough tests when I will be happy with code quality):

Step v2 v2 with changes
Building production JavaScript bundles 30s - 35s 3s - 4s
Building static HTML for pages ~30s 5s - 6s

@jquense
Copy link
Contributor

jquense commented Mar 16, 2018

this is awesome

@KyleAMathews
Copy link
Contributor

KyleAMathews commented Mar 16, 2018

@pieh woah! That's huge!!! Bigger impact than I expected actually. It'll be even faster with webpack 4 & multi-core support for HTML building (we should do that post-v2 launch).

@KyleAMathews
Copy link
Contributor

Tweet for this is up to 319 likes haha https://twitter.com/gatsbyjs/status/974507205121617920

Great job @m-allanson!

@KyleAMathews
Copy link
Contributor

@pieh yeah, just use Node's built-in crypto functionality to create a hash of the query result and use that to write out the file. Then store that as part of the pages object so pages-writer has access to it.

@KyleAMathews KyleAMathews changed the title [v2][WIP] Custom JSON loader [v2][WIP] Change how we load GraphQL query results Mar 17, 2018
ws.pushResult(result)
const sockets = ws.instance()
if (sockets) {
sockets.emit(`queryResult`, result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only emit the resultJSON — for large sites, this could turn into megabytes of JSON.

@KyleAMathews
Copy link
Contributor

Freecodecamp is running into scaling troubles so let's test these new changes on their wip branch and get them upgraded as soon as we can things stable https://github.com/freeCodeCamp/guides/tree/feat/new-challenge-stubs

…e fetch to not create additional request, with any `cache` or `mode` configuration
@@ -111,14 +111,6 @@ function shouldUpdateScroll(prevRouterProps, { location: { pathname } }) {
return true
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this moved elsewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removal probably shouldn't be in this PR - this is dead code even in master - https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/cache-dir/root.js#L120 addNotFoundRoute is defined but not used anywhere and so is noMatch after removal of previous function

@ghost ghost assigned KyleAMathews Mar 29, 2018
pieh and others added 5 commits March 29, 2018 09:38
…cessing (#4785)

* Don't emit new file node until previous is finished processing

This is an experiment to use
[xstate](http://davidkpiano.github.io/xstate/docs/#/) to setup state
machines to better handle complex state changes as we sometimes have.

Ideally this happens in core and then gatsby-source-filesystem
just has a simple queue and emits a new file node every time
the system returns to idle.

In a future refactor we'll do that plus refactor other parts of core
that should be handled in a state machine e.g. pages-query-runner.js

This PR also reinforced the need for us to implement
[tracing](https://github.com/jaegertracing/jaeger) in core / plugins
as that'd make it far far easier to understand what's happening and
when.

* Document state machine and remove extraneous Chokidar states

* Remove console.log
* Don't emit new file node until previous is finished processing

This is an experiment to use
[xstate](http://davidkpiano.github.io/xstate/docs/#/) to setup state
machines to better handle complex state changes as we sometimes have.

Ideally this happens in core and then gatsby-source-filesystem
just has a simple queue and emits a new file node every time
the system returns to idle.

In a future refactor we'll do that plus refactor other parts of core
that should be handled in a state machine e.g. pages-query-runner.js

This PR also reinforced the need for us to implement
[tracing](https://github.com/jaegertracing/jaeger) in core / plugins
as that'd make it far far easier to understand what's happening and
when.

* Document state machine and remove extraneous Chokidar states

* Remove console.log

* Only log file events if we're past bootstrap
@pieh
Copy link
Contributor

pieh commented Apr 5, 2018

from my part - for minimal needed functionality this only need those 2 PRs - #4837 and #4850

pieh and others added 5 commits April 5, 2018 11:39
* prevent adding duplicate redirects

* don't write new `redirects.json` if redirects didn't change

prevents webpack recompilation on data change
* dont emit results for layouts

* [develop] store query results in memory, read json data from file only if we don't have it stored yet (we didn't run this query, but results are cached)
Query running is sadly not very ludicrous right now on gatsbyjs.org —
not sure why — each markdown file change causes ~20 queries to run but
even with prioritizing the active page's query, it's still ~2 seconds
before the page updates.

This sort of thing will be much easier to debug with tracing support.
@KyleAMathews KyleAMathews changed the title [v2][WIP] Change how we load GraphQL query results [v2] improve how we load GraphQL query results in development/production (aka Ludicrous Mode) Apr 6, 2018
@KyleAMathews KyleAMathews merged commit cfa019d into v2 Apr 6, 2018
@ghost ghost removed the review label Apr 6, 2018
@KyleAMathews
Copy link
Contributor

🎉 thanks @m-allanson and @pieh!!!!!!!

This is a huge improvement for development and large site builds. So happy to get this in :-D

@KyleAMathews
Copy link
Contributor

I'll push out a new alpha so people can try this out.

s.on(`disconnect`, s => {
leaveRoom(activePath)
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking here - I think after removing this handler, when browser tab will be closed (so unregisterPath event won't be emitted), active path won't get unregistered and therefore query for that path will continue to be prioritized

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... that's right — lemme add this back real quick

@HriBB
Copy link

HriBB commented Apr 8, 2018

So cool!!! Great job guys! BTW how can you do a preview as you type? Mine only updates after I save the file.

I guess now, we should be able to do a live preview on production:

  • write a source plugin for some custom GraphQL schema
  • handle source nodes CRUD with GraphQL subscriptions
  • run gatsby develop in production so only client can see it

@KyleAMathews
Copy link
Contributor

@HriBB @m-allanson used VSCode with AutoSave enabled and autosavedelay set to 10 (milliseconds)

@m-allanson m-allanson deleted the json-loader branch August 6, 2018 15:28
mwfrost pushed a commit to mwfrost/gatsby that referenced this pull request Apr 20, 2023
…ion (aka Ludicrous Mode) (gatsbyjs#4555)

* Create placeholder JSON store

* Rename

* Websocket placeholder

* Push query results JSON over websockets

* More descriptive variable name

* Fix queries being overwritten

* Remove eslint-disable flag

* Remove junk

* test require error fix for windows

* dont require json data in sync-require

* dont add layout data to json array multiple times

* initial async loading

* revert saving json directly to public for now

* updated production-app to sync with prop name change in ComponentRenderer

* we load json data via json-loader component in develop and not handling it with webpack import/require

* hashes for json files

* fix preloading, use xhr instead of fetch - for some reason can't force fetch to not create additional request, with any `cache` or `mode` configuration

* dont use full paths in dataPath - remove static/d/ path and .json ext - results in smaller app bundle especially with large ammount of pages

* Enable cached query results to be loaded

* Don't dump all query results out to the client

Instead only push results out if the data is for a path that's currently
being viewed in a client.

* fix preload link to json data

* remove not used function

* remove more not used code

* Update to latest webpack/mini-css-extract-plugin

* don't write new (a)sync-requires.js if components didn't change (gatsbyjs#4759)

* create just one websocket client (gatsbyjs#4763)

* Filter out duplicate query jobs and create secondary queue for jobs if path already has query in flight

* [json-loader] Don't emit new file node until previous is finished processing (gatsbyjs#4785)

* Don't emit new file node until previous is finished processing

This is an experiment to use
[xstate](http://davidkpiano.github.io/xstate/docs/#/) to setup state
machines to better handle complex state changes as we sometimes have.

Ideally this happens in core and then gatsby-source-filesystem
just has a simple queue and emits a new file node every time
the system returns to idle.

In a future refactor we'll do that plus refactor other parts of core
that should be handled in a state machine e.g. pages-query-runner.js

This PR also reinforced the need for us to implement
[tracing](https://github.com/jaegertracing/jaeger) in core / plugins
as that'd make it far far easier to understand what's happening and
when.

* Document state machine and remove extraneous Chokidar states

* Remove console.log

* [json-loader] Only log file events if we're past bootstrap (gatsbyjs#4826)

* Don't emit new file node until previous is finished processing

This is an experiment to use
[xstate](http://davidkpiano.github.io/xstate/docs/#/) to setup state
machines to better handle complex state changes as we sometimes have.

Ideally this happens in core and then gatsby-source-filesystem
just has a simple queue and emits a new file node every time
the system returns to idle.

In a future refactor we'll do that plus refactor other parts of core
that should be handled in a state machine e.g. pages-query-runner.js

This PR also reinforced the need for us to implement
[tracing](https://github.com/jaegertracing/jaeger) in core / plugins
as that'd make it far far easier to understand what's happening and
when.

* Document state machine and remove extraneous Chokidar states

* Remove console.log

* Only log file events if we're past bootstrap

* [json-loader] dont recompile on data change - part 2 (gatsbyjs#4837)

* prevent adding duplicate redirects

* don't write new `redirects.json` if redirects didn't change

prevents webpack recompilation on data change

* [json-loader] develop - reading results from file improvments (gatsbyjs#4850)

* dont emit results for layouts

* [develop] store query results in memory, read json data from file only if we don't have it stored yet (we didn't run this query, but results are cached)

* Add query prioritization based on what page(s) user(s) are on

Query running is sadly not very ludicrous right now on gatsbyjs.org —
not sure why — each markdown file change causes ~20 queries to run but
even with prioritizing the active page's query, it's still ~2 seconds
before the page updates.

This sort of thing will be much easier to debug with tracing support.

* Add initial forward slash

* Actually this is how we add back the initial forward slash
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

Successfully merging this pull request may close these issues.

6 participants