Fix /outfits/new init + add more SSR
Whew, setting up a cute GraphQL SSR system! I feel like it strikes a good balance of not having actually too many moving parts, though it's still a bit extensive for the problem we're solving 😅
Anyway, by doing SSR at _all_, we solve the problem where Next's "Automatic Static Optimization" was causing problems by setting the outfit state to the default at the start of the page load.
So I figured, why not try to SSR things _good_?
Now, when you navigate to the /outfits/new page, Next.js will go get the necessary GraphQL data to show the image before even putting the page into view. This makes the image show up all snappy-like! (when images.neopets.com is behaving :p)
We could do this with the stuff in the items panel too, but it's a tiny bit more annoying in the code right now, so I'm just gonna not worry about it and see how this performs in practice!
This change _doesn't_ include making the images actually show up before JS loads in, I assume because our JS code tries to validate that the images have loaded before fading them in on the page. Idk if we want to do something smarter there for the SSR case, to try to get them loading in faster!
2022-09-15 02:46:14 -07:00
|
|
|
const { InMemoryCache } = require("@apollo/client");
|
|
|
|
|
const { ApolloServer, gql } = require("apollo-server");
|
|
|
|
|
const { config } = require("./index");
|
|
|
|
|
|
|
|
|
|
const server = new ApolloServer(config);
|
|
|
|
|
|
|
|
|
|
async function loadGraphqlQuery({ query, variables = {} }) {
|
|
|
|
|
// Edit the query to serve our needs, then send a local in-memory request to
|
|
|
|
|
// a simple `ApolloServer` instance just for SSR.
|
|
|
|
|
const convertedQuery = addTypenameToSelections(removeClientOnlyFields(query));
|
|
|
|
|
const { data, errors } = await server.executeOperation({
|
|
|
|
|
query: convertedQuery,
|
|
|
|
|
variables,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// To get the cache data, we build a new temporary cache object, write this
|
|
|
|
|
// query result to it, and dump it out. (Building a normalized cache state is
|
|
|
|
|
// really tricky, this simplifies it a lot without bringing in the weight of
|
|
|
|
|
// a whole client!)
|
|
|
|
|
const cache = new InMemoryCache();
|
|
|
|
|
cache.writeQuery({ query, variables, data });
|
2022-09-15 03:01:56 -07:00
|
|
|
const graphqlState = cache.extract();
|
Fix /outfits/new init + add more SSR
Whew, setting up a cute GraphQL SSR system! I feel like it strikes a good balance of not having actually too many moving parts, though it's still a bit extensive for the problem we're solving 😅
Anyway, by doing SSR at _all_, we solve the problem where Next's "Automatic Static Optimization" was causing problems by setting the outfit state to the default at the start of the page load.
So I figured, why not try to SSR things _good_?
Now, when you navigate to the /outfits/new page, Next.js will go get the necessary GraphQL data to show the image before even putting the page into view. This makes the image show up all snappy-like! (when images.neopets.com is behaving :p)
We could do this with the stuff in the items panel too, but it's a tiny bit more annoying in the code right now, so I'm just gonna not worry about it and see how this performs in practice!
This change _doesn't_ include making the images actually show up before JS loads in, I assume because our JS code tries to validate that the images have loaded before fading them in on the page. Idk if we want to do something smarter there for the SSR case, to try to get them loading in faster!
2022-09-15 02:46:14 -07:00
|
|
|
|
|
|
|
|
// We return the data, errors, and cache state: we figure callers will almost
|
|
|
|
|
// always want the errors and state, and may also want the data!
|
2022-09-15 03:01:56 -07:00
|
|
|
return { data, errors, graphqlState };
|
Fix /outfits/new init + add more SSR
Whew, setting up a cute GraphQL SSR system! I feel like it strikes a good balance of not having actually too many moving parts, though it's still a bit extensive for the problem we're solving 😅
Anyway, by doing SSR at _all_, we solve the problem where Next's "Automatic Static Optimization" was causing problems by setting the outfit state to the default at the start of the page load.
So I figured, why not try to SSR things _good_?
Now, when you navigate to the /outfits/new page, Next.js will go get the necessary GraphQL data to show the image before even putting the page into view. This makes the image show up all snappy-like! (when images.neopets.com is behaving :p)
We could do this with the stuff in the items panel too, but it's a tiny bit more annoying in the code right now, so I'm just gonna not worry about it and see how this performs in practice!
This change _doesn't_ include making the images actually show up before JS loads in, I assume because our JS code tries to validate that the images have loaded before fading them in on the page. Idk if we want to do something smarter there for the SSR case, to try to get them loading in faster!
2022-09-15 02:46:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* addTypenameToSelections recursively adds __typename to every selection set
|
|
|
|
|
* in the query, and returns a copy. This enables us to use the query data to
|
|
|
|
|
* populate a cache!
|
|
|
|
|
*/
|
|
|
|
|
function addTypenameToSelections(node) {
|
|
|
|
|
if (node.kind === "SelectionSet") {
|
|
|
|
|
return {
|
|
|
|
|
...node,
|
|
|
|
|
selections: [
|
|
|
|
|
{
|
|
|
|
|
kind: "Field",
|
|
|
|
|
name: {
|
|
|
|
|
kind: "Name",
|
|
|
|
|
value: "__typename",
|
|
|
|
|
arguments: [],
|
|
|
|
|
directives: [],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
...node.selections.map((s) => addTypenameToSelections(s)),
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
} else if (node.selectionSet != null) {
|
|
|
|
|
return {
|
|
|
|
|
...node,
|
|
|
|
|
selectionSet: addTypenameToSelections(node.selectionSet),
|
|
|
|
|
};
|
|
|
|
|
} else if (node.kind === "Document") {
|
|
|
|
|
return {
|
|
|
|
|
...node,
|
|
|
|
|
definitions: node.definitions.map((d) => addTypenameToSelections(d)),
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* removeClientOnlyFields recursively removes any fields marked with `@client`
|
|
|
|
|
* in the given GraphQL document node, and returns a new copy. This enables us
|
|
|
|
|
* to borrow queries and fragments from the client, and ignore the fields they
|
|
|
|
|
* won't need preloaded for SSR. (This isn't just an optimization: the server
|
|
|
|
|
* can't handle the `@client` directive and the query will fail if present!)
|
|
|
|
|
*/
|
|
|
|
|
function removeClientOnlyFields(node) {
|
|
|
|
|
if (node.kind === "SelectionSet") {
|
|
|
|
|
return {
|
|
|
|
|
...node,
|
|
|
|
|
selections: node.selections
|
|
|
|
|
.filter(
|
|
|
|
|
(selection) =>
|
|
|
|
|
!(
|
|
|
|
|
selection.kind === "Field" &&
|
|
|
|
|
selection.directives.some((d) => d.name.value === "client")
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.map((selection) => removeClientOnlyFields(selection)),
|
|
|
|
|
};
|
|
|
|
|
} else if (node.selectionSet != null) {
|
|
|
|
|
return { ...node, selectionSet: removeClientOnlyFields(node.selectionSet) };
|
|
|
|
|
} else if (node.kind === "Document") {
|
|
|
|
|
return {
|
|
|
|
|
...node,
|
|
|
|
|
definitions: node.definitions.map((d) => removeClientOnlyFields(d)),
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
loadGraphqlQuery,
|
|
|
|
|
gql,
|
|
|
|
|
};
|