From a21fd774a07549efed5ffb91101362990fab73f3 Mon Sep 17 00:00:00 2001 From: MMT-LD Date: Wed, 16 Dec 2020 11:35:08 +0000 Subject: [PATCH 1/9] feature/vanilla-emotion - add an example of vanilla emotion --- examples/with-emotion-vanilla/.gitignore | 34 ++++++ examples/with-emotion-vanilla/README.md | 24 +++++ examples/with-emotion-vanilla/package.json | 17 +++ examples/with-emotion-vanilla/pages/_app.js | 20 ++++ .../with-emotion-vanilla/pages/_document.js | 39 +++++++ examples/with-emotion-vanilla/pages/index.js | 30 ++++++ .../with-emotion-vanilla/shared/emotion.js | 12 +++ .../with-emotion-vanilla/shared/renderer.js | 13 +++ .../with-emotion-vanilla/shared/styles.js | 102 ++++++++++++++++++ 9 files changed, 291 insertions(+) create mode 100644 examples/with-emotion-vanilla/.gitignore create mode 100644 examples/with-emotion-vanilla/README.md create mode 100644 examples/with-emotion-vanilla/package.json create mode 100644 examples/with-emotion-vanilla/pages/_app.js create mode 100644 examples/with-emotion-vanilla/pages/_document.js create mode 100644 examples/with-emotion-vanilla/pages/index.js create mode 100644 examples/with-emotion-vanilla/shared/emotion.js create mode 100644 examples/with-emotion-vanilla/shared/renderer.js create mode 100644 examples/with-emotion-vanilla/shared/styles.js diff --git a/examples/with-emotion-vanilla/.gitignore b/examples/with-emotion-vanilla/.gitignore new file mode 100644 index 0000000000000..1437c53f70bc2 --- /dev/null +++ b/examples/with-emotion-vanilla/.gitignore @@ -0,0 +1,34 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env.local +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel diff --git a/examples/with-emotion-vanilla/README.md b/examples/with-emotion-vanilla/README.md new file mode 100644 index 0000000000000..f43e633dfc36b --- /dev/null +++ b/examples/with-emotion-vanilla/README.md @@ -0,0 +1,24 @@ +# Emotion Vanilla Example + +Extract and inline critical css with +[emotion](https://github.com/emotion-js/emotion/tree/master/packages/emotion), +[@emotion/server](https://github.com/emotion-js/emotion/tree/master/packages/server), +[@emotion/css](https://github.com/emotion-js/emotion/tree/master/packages/css) + +## Deploy your own + +Deploy the example using [Vercel](https://vercel.com): + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-emotion-vanilla) + +## How to use + +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: + +```bash +npx create-next-app --example with-emotion-vanilla with-emotion-vanilla-app +# or +yarn create next-app --example with-emotion-vanilla with-emotion-vanilla-app +``` + +Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). diff --git a/examples/with-emotion-vanilla/package.json b/examples/with-emotion-vanilla/package.json new file mode 100644 index 0000000000000..1c16666bcf915 --- /dev/null +++ b/examples/with-emotion-vanilla/package.json @@ -0,0 +1,17 @@ +{ + "name": "with-emotion-vanilla", + "version": "1.0.0", + "scripts": { + "dev": "next", + "build": "next build", + "start": "next start" + }, + "license": "MIT", + "dependencies": { + "@emotion/css": "11.0.0", + "@emotion/server": "11.0.0", + "next": "latest", + "react": "^17.0.1", + "react-dom": "^17.0.1" + } +} diff --git a/examples/with-emotion-vanilla/pages/_app.js b/examples/with-emotion-vanilla/pages/_app.js new file mode 100644 index 0000000000000..6cfdd2424520f --- /dev/null +++ b/examples/with-emotion-vanilla/pages/_app.js @@ -0,0 +1,20 @@ +import * as React from 'react' +import { hydrate } from '../shared/emotion' + +// Adds server generated styles to emotion cache. +// this needs to come before the app mounts +// We use query selector here as setting on window results in emotion error `Cannot read property 'forEach' of undefined` +if (typeof window !== 'undefined') { + const ids = JSON.parse( + document + .querySelector('[data-emotion-ssr]') + .getAttribute('data-emotion-ssr') + ) + hydrate(ids) +} + +const App = ({ Component, pageProps }) => { + return +} + +export default App diff --git a/examples/with-emotion-vanilla/pages/_document.js b/examples/with-emotion-vanilla/pages/_document.js new file mode 100644 index 0000000000000..1f0a2e761ac34 --- /dev/null +++ b/examples/with-emotion-vanilla/pages/_document.js @@ -0,0 +1,39 @@ +import Document, { Html, Head, Main, NextScript } from 'next/document' +import * as React from 'react' +import { renderStatic } from '../shared/renderer' + +// Please note: - the reason for data-emotion-ssr={JSON.stringify(ids)} is because +// hydrate can't find the ids if we set them using window.__ids +// adding this and looking for it in _app.js allows it to always be ready so we just parse that attribute to get the ids array +export default class AppDocument extends Document { + static async getInitialProps(ctx) { + const page = await ctx.renderPage() + const { css, ids } = await renderStatic(() => page.html) + const initialProps = await Document.getInitialProps(ctx) + return { + ...initialProps, + styles: ( + + {initialProps.styles} +