Skip to content

Commit

Permalink
Add JS wrapper to CSS to support Serverless target
Browse files Browse the repository at this point in the history
Resolves #281
  • Loading branch information
iaincollins committed Jun 21, 2020
1 parent 6eeed21 commit 52f2dd5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "next-auth",
"version": "2.0.0-beta.85",
"version": "2.0.0-beta.86",
"description": "An authentication library for Next.js",
"repository": "https://github.com/iaincollins/next-auth.git",
"author": "Iain Collins <me@iaincollins.com>",
"main": "index.js",
"scripts": {
"build": "npm run build:js && npm run build:css",
"build:js": "babel src --out-dir dist",
"build:css": "postcss src/**/*.css --base src --dir dist",
"build:css": "postcss src/**/*.css --base src --dir dist && node scripts/wrap-css.js",
"watch": "npm run watch:js | npm run watch:css",
"watch:js": "babel --watch src --out-dir dist",
"watch:css": "postcss --watch src/**/*.css --base src --dir dist",
Expand Down
18 changes: 18 additions & 0 deletions scripts/wrap-css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Serverless target in Next.js does work if you try to read in files at runtime
// that are not JavaScript or JSON (e.g. CSS files).
// https://github.com/iaincollins/next-auth/issues/281
//
// To work around this issue, this script is a manual step that wraps CSS in a
// JavaScript file that has the compiled CSS embedded in it, and exports only
// a function that returns the CSS as a string.
const fs = require('fs')
const path = require('path')

const pathToCssJs = path.join(__dirname, '../dist/css/index.js')
const pathToCss = path.join(__dirname, '../dist/css/index.css')

const css = fs.readFileSync(pathToCss, 'utf8')
const cssWithEscapedQuotes = css.replace(/"/gm, '\\"')
const js = `module.exports = function() { return "${cssWithEscapedQuotes}" }`

fs.writeFileSync(pathToCssJs, js)
10 changes: 10 additions & 0 deletions src/css/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// To support serverless targets (which don't work if you try to read in things
// like CSS files at run time) this file is replaced in production builds with
// a function that returns compiled CSS (embedded as a string in the function).
import fs from 'fs'
import path from 'path'

const pathToCss = path.join(__dirname, '/index.css')
const css = fs.readFileSync(pathToCss, 'utf8')

export default () => css
6 changes: 2 additions & 4 deletions src/server/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import signin from './signin'
import signout from './signout'
import verifyRequest from './verify-request'
import error from './error'

// Future releases will support customization (via inline or external CSS)
const defaultStyles = fs.readFileSync(path.join(__dirname, '/../../css/index.css'), 'utf8')
import css from '../../css'

function render (req, res, page, props, done) {
let html = ''
Expand All @@ -29,7 +27,7 @@ function render (req, res, page, props, done) {
}

res.setHeader('Content-Type', 'text/html')
res.send(`<!DOCTYPE html><head><style type="text/css">${defaultStyles}</style><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><div class="page">${html}</div></body></html>`)
res.send(`<!DOCTYPE html><head><style type="text/css">${css()}</style><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><div class="page">${html}</div></body></html>`)
done()
}

Expand Down

1 comment on commit 52f2dd5

@vercel
Copy link

@vercel vercel bot commented on 52f2dd5 Jun 21, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.