From 5c89bc386e3065d9481f484fbdcfaecf18752e9a Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Thu, 11 May 2017 11:46:43 -0700 Subject: [PATCH] [1.0] WIP Upgrade React Router to v4 (#940) * WIP RR v4 working in development * Add scrolling support + small refactors * Add clientside 404 support * Use more generic id for root div for Gatsby * Avoid unneeded dependency on create-react-class * Use fork of catch-links to avoid its dependency on the url module which adds ~25kb of JS https://github.com/jackmoore/catch-links/blob/aedc104ae5b1031b325398997827c76bb08d9f4c/index.js * Remove react-ga dependency as it adds unneeded weight * Static rendering working and production client entry sorta working * Get router working in production w/ lazy loading of bundles * Make this.props.children a function in layouts so can control which page is rendered as child * Add path-to-regexp to list of modules that should always be in commons * Expose navigateTo on gatsby-link for programmatic navigation * Stop using term 'routes' * Remove old routes generation code * Rename file * Add support for client-only paths & example site for this * Format + remove debugging --- examples/client-only-paths/.eslintrc | 8 + examples/client-only-paths/README.md | 6 + examples/client-only-paths/gatsby-config.js | 14 + examples/client-only-paths/gatsby-node.js | 19 ++ examples/client-only-paths/package.json | 29 ++ examples/client-only-paths/src/html.js | 56 ++++ .../client-only-paths/src/layouts/index.js | 30 +++ examples/client-only-paths/src/pages/index.js | 114 ++++++++ examples/client-only-paths/src/pages/main.css | 10 + .../client-only-paths/src/utils/typography.js | 38 +++ examples/gatsbygram/gatsby-node.js | 2 +- examples/gatsbygram/src/components/modal.js | 17 +- examples/gatsbygram/src/html.js | 2 +- .../src/layouts/{default.js => index.js} | 33 ++- examples/gatsbygram/src/pages/index.js | 3 +- examples/hn/src/html.js | 2 +- examples/hn/src/layouts/default.js | 2 +- examples/using-drupal/src/html.js | 2 +- .../src/layouts/{default.js => index.js} | 2 +- examples/using-drupal/src/pages/index.js | 2 +- packages/gatsby-link/package.json | 4 +- packages/gatsby-link/src/index.js | 76 +++--- packages/gatsby-link/yarn.lock | 13 +- packages/gatsby-plugin-catch-links/.gitignore | 1 + .../gatsby-plugin-catch-links/package.json | 3 - .../src/catch-links.js | 43 +++ .../src/gatsby-browser.js | 8 +- .../gatsby-plugin-google-analytics/.gitignore | 1 + .../package.json | 3 - .../src/gatsby-browser.js | 13 +- .../src/gatsby-ssr.js | 17 ++ packages/gatsby-plugin-preact/package.json | 4 +- .../src/on-node-create.js | 4 +- .../src/parse.js | 12 +- .../gatsby/lib/query-runner/pages-writer.js | 137 ++++++++++ packages/gatsby/package.json | 7 +- packages/gatsby/src/bootstrap/index.js | 5 +- packages/gatsby/src/cache-dir/app.js | 4 +- .../gatsby/src/cache-dir/production-app.js | 185 ++++++++++--- packages/gatsby/src/cache-dir/root.js | 101 +++++-- packages/gatsby/src/cache-dir/static-entry.js | 235 ++++++++-------- packages/gatsby/src/joi-schemas/joi.js | 1 + packages/gatsby/src/query-runner/index.js | 2 +- .../gatsby/src/query-runner/route-writer.js | 251 ------------------ .../gatsby/src/schema/build-node-types.js | 2 +- .../src/schema/infer-graphql-input-fields.js | 2 +- .../gatsby/src/schema/infer-graphql-type.js | 4 +- packages/gatsby/src/schema/node-interface.js | 2 +- packages/gatsby/src/schema/site-schema.js | 6 +- packages/gatsby/src/utils/build.js | 6 - packages/gatsby/src/utils/webpack.config.js | 11 +- .../connection/__tests__/arrayconnection.js | 12 +- .../src/connection/__tests__/connection.js | 11 +- .../src/connection/arrayconnection.js | 17 +- .../src/connection/connection.js | 14 +- .../src/connection/connectiontypes.js | 24 +- www/src/html.js | 2 +- www/src/layouts/{default.js => index.js} | 2 +- www/src/pages/index.js | 2 +- www/src/templates/template-blog-post.js | 1 - 60 files changed, 1041 insertions(+), 598 deletions(-) create mode 100644 examples/client-only-paths/.eslintrc create mode 100644 examples/client-only-paths/README.md create mode 100644 examples/client-only-paths/gatsby-config.js create mode 100644 examples/client-only-paths/gatsby-node.js create mode 100644 examples/client-only-paths/package.json create mode 100644 examples/client-only-paths/src/html.js create mode 100644 examples/client-only-paths/src/layouts/index.js create mode 100644 examples/client-only-paths/src/pages/index.js create mode 100644 examples/client-only-paths/src/pages/main.css create mode 100644 examples/client-only-paths/src/utils/typography.js rename examples/gatsbygram/src/layouts/{default.js => index.js} (87%) rename examples/using-drupal/src/layouts/{default.js => index.js} (97%) create mode 100644 packages/gatsby-plugin-catch-links/src/catch-links.js create mode 100644 packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js create mode 100644 packages/gatsby/lib/query-runner/pages-writer.js delete mode 100644 packages/gatsby/src/query-runner/route-writer.js rename www/src/layouts/{default.js => index.js} (99%) diff --git a/examples/client-only-paths/.eslintrc b/examples/client-only-paths/.eslintrc new file mode 100644 index 0000000000000..aadde9c0aa03d --- /dev/null +++ b/examples/client-only-paths/.eslintrc @@ -0,0 +1,8 @@ +{ + "env": { + "browser": true + }, + "globals": { + "graphql": false + } +} \ No newline at end of file diff --git a/examples/client-only-paths/README.md b/examples/client-only-paths/README.md new file mode 100644 index 0000000000000..29fdaa2dc6d56 --- /dev/null +++ b/examples/client-only-paths/README.md @@ -0,0 +1,6 @@ +# Client-only paths + +https://client-only-paths.gatsbyjs.org + +Example site that demostrates how to build Gatsby sites +with paths that are client-only. diff --git a/examples/client-only-paths/gatsby-config.js b/examples/client-only-paths/gatsby-config.js new file mode 100644 index 0000000000000..67d69843c1785 --- /dev/null +++ b/examples/client-only-paths/gatsby-config.js @@ -0,0 +1,14 @@ +module.exports = { + siteMetadata: { + title: `Client only paths`, + }, + plugins: [ + { + resolve: `gatsby-plugin-google-analytics`, + options: { + trackingId: `UA-93349937-2`, + }, + }, + `gatsby-plugin-offline`, + ], +} diff --git a/examples/client-only-paths/gatsby-node.js b/examples/client-only-paths/gatsby-node.js new file mode 100644 index 0000000000000..e217a7856f4e9 --- /dev/null +++ b/examples/client-only-paths/gatsby-node.js @@ -0,0 +1,19 @@ +const _ = require(`lodash`) +const Promise = require(`bluebird`) +const path = require(`path`) +const slash = require(`slash`) + +// Implement the Gatsby API “onUpsertPage”. This is +// called after every page is created. +exports.onUpsertPage = ({ page, boundActionCreators }) => { + const { upsertPage } = boundActionCreators + return new Promise((resolve, reject) => { + // Make the front page match everything client side. + // Normally your paths should be a bit more judicious. + if (page.path === `/` && !page.matchPath) { + page.matchPath = `/:path` + upsertPage(page) + } + resolve() + }) +} diff --git a/examples/client-only-paths/package.json b/examples/client-only-paths/package.json new file mode 100644 index 0000000000000..878fb5a187930 --- /dev/null +++ b/examples/client-only-paths/package.json @@ -0,0 +1,29 @@ +{ + "name": "gatsby-example-using-drupal", + "private": true, + "description": "Gatsby example site using the Drupal source plugin", + "version": "1.0.0", + "author": "Kyle Mathews ", + "dependencies": { + "gatsby": "canary", + "gatsby-link": "canary", + "gatsby-plugin-google-analytics": "canary", + "gatsby-plugin-offline": "canary", + "gatsby-source-drupal": "canary", + "lodash": "^4.16.4", + "react-addons-css-transition-group": "^15.5.2", + "react-typography": "^0.15.0", + "slash": "^1.0.0", + "typography": "^0.15.8", + "typography-breakpoint-constants": "^0.14.0" + }, + "keywords": [ + "gatsby" + ], + "license": "MIT", + "main": "n/a", + "scripts": { + "develop": "gatsby develop", + "build": "gatsby build" + } +} diff --git a/examples/client-only-paths/src/html.js b/examples/client-only-paths/src/html.js new file mode 100644 index 0000000000000..5415c9c674e32 --- /dev/null +++ b/examples/client-only-paths/src/html.js @@ -0,0 +1,56 @@ +import React from "react" +import { TypographyStyle } from "react-typography" +import typography from "./utils/typography" + +let stylesStr +if (process.env.NODE_ENV === `production`) { + try { + stylesStr = require(`!raw-loader!../public/styles.css`) + } catch (e) { + console.log(e) + } +} + +module.exports = React.createClass({ + render() { + let css + if (process.env.NODE_ENV === `production`) { + css = ( +