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

[legacy-framework] Add helpful error when trying blitz export with blitz auth (patch) #2435

Merged
merged 5 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ examples/auth2
.ultra.cache.json
db.sqlite-journal
test/integration/**/db.json
test/**/*/out
9 changes: 9 additions & 0 deletions nextjs/packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ export default async function build(
customAppGetInitialProps,
namedExports,
isNextImageImported,
isBlitzSessionMiddlewareUsed,
hasSsrAmpPages,
hasNonStaticErrorPage,
} = await staticCheckSpan.traceAsyncFn(async () => {
Expand Down Expand Up @@ -714,6 +715,8 @@ export default async function build(
// eslint-disable-next-line no-shadow
let isNextImageImported: boolean | undefined
// eslint-disable-next-line no-shadow
let isBlitzSessionMiddlewareUsed: boolean | undefined
// eslint-disable-next-line no-shadow
let hasSsrAmpPages = false

const computedManifestData = await computeFromManifest(
Expand Down Expand Up @@ -778,6 +781,10 @@ export default async function build(
isNextImageImported = true
}

if (workerResult.isBlitzSessionMiddlewareUsed) {
isBlitzSessionMiddlewareUsed = true
}

if (workerResult.hasStaticProps) {
ssgPages.add(page)
isSsg = true
Expand Down Expand Up @@ -856,6 +863,7 @@ export default async function build(
customAppGetInitialProps: await customAppGetInitialPropsPromise,
namedExports: await namedExportsPromise,
isNextImageImported,
isBlitzSessionMiddlewareUsed,
hasSsrAmpPages,
hasNonStaticErrorPage: await nonStaticErrorPagePromise,
}
Expand Down Expand Up @@ -1516,6 +1524,7 @@ export default async function build(
hasExportPathMap: typeof config.exportPathMap === 'function',
exportTrailingSlash: config.trailingSlash === true,
isNextImageImported: isNextImageImported === true,
isBlitzSessionMiddlewareUsed: isBlitzSessionMiddlewareUsed === true,
}),
'utf8'
)
Expand Down
4 changes: 4 additions & 0 deletions nextjs/packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ export async function isPageStatic(
encodedPrerenderRoutes?: string[]
prerenderFallback?: boolean | 'blocking'
isNextImageImported?: boolean
isBlitzSessionMiddlewareUsed?: boolean
}> {
const isPageStaticSpan = trace('is-page-static-utils', parentId)
return isPageStaticSpan.traceAsyncFn(async () => {
Expand Down Expand Up @@ -854,6 +855,8 @@ export async function isPageStatic(
}

const isNextImageImported = (global as any).__NEXT_IMAGE_IMPORTED
const isBlitzSessionMiddlewareUsed = (global as any)
.__BLITZ_SESSION_MIDDLEWARE_USED
const config = mod.config || {}
return {
isStatic: !hasStaticProps && !hasGetInitialProps && !hasServerProps,
Expand All @@ -865,6 +868,7 @@ export async function isPageStatic(
hasStaticProps,
hasServerProps,
isNextImageImported,
isBlitzSessionMiddlewareUsed,
}
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return {}
Expand Down
14 changes: 13 additions & 1 deletion nextjs/packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ export default async function exportApp(
}

if (!options.buildExport) {
const { isNextImageImported } = await nextExportSpan
const {
isNextImageImported,
isBlitzSessionMiddlewareUsed,
} = await nextExportSpan
.traceChild('is-next-image-imported')
.traceAsyncFn(() =>
promises
Expand All @@ -337,6 +340,15 @@ export default async function exportApp(
.catch(() => ({}))
)

if (isBlitzSessionMiddlewareUsed) {
throw new Error(
`Blitz sessionMiddleware is not compatible with \`blitz export\`.
Possible solutions:
- Use \`blitz start\` to run a server, which supports middleware.
- Remove \`sessionMiddleware\` import from \`blitz.config.js\`.\n`
)
}

if (isNextImageImported && loader === 'default' && !hasNextSupport) {
throw new Error(
`Image Optimization using Next.js' default loader is not compatible with \`next export\`.
Expand Down
5 changes: 5 additions & 0 deletions nextjs/test/integration/export-intent/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Application Export Intent Output', () => {
Object {
"exportTrailingSlash": false,
"hasExportPathMap": false,
"isBlitzSessionMiddlewareUsed": false,
"isNextImageImported": false,
"version": 1,
}
Expand Down Expand Up @@ -71,6 +72,7 @@ describe('Application Export Intent Output', () => {
Object {
"exportTrailingSlash": false,
"hasExportPathMap": true,
"isBlitzSessionMiddlewareUsed": false,
"isNextImageImported": false,
"version": 1,
}
Expand Down Expand Up @@ -112,6 +114,7 @@ describe('Application Export Intent Output', () => {
Object {
"exportTrailingSlash": true,
"hasExportPathMap": false,
"isBlitzSessionMiddlewareUsed": false,
"isNextImageImported": false,
"version": 1,
}
Expand Down Expand Up @@ -153,6 +156,7 @@ describe('Application Export Intent Output', () => {
Object {
"exportTrailingSlash": false,
"hasExportPathMap": false,
"isBlitzSessionMiddlewareUsed": false,
"isNextImageImported": false,
"version": 1,
}
Expand Down Expand Up @@ -193,6 +197,7 @@ describe('Application Export Intent Output', () => {
Object {
"exportTrailingSlash": false,
"hasExportPathMap": false,
"isBlitzSessionMiddlewareUsed": false,
"isNextImageImported": false,
"version": 1,
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export class Export extends Command {

static flags = {
help: flags.help({char: "h"}),
outdir: flags.string({
char: "o",
description: "set the output dir (defaults to 'out')",
}),
}

async run() {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/server/auth/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export const sessionMiddleware = (sessionConfig: Partial<SessionConfig> = {}): M
sessionConfig.isAuthorized,
"You must provide an authorization implementation to sessionMiddleware as isAuthorized(userRoles, input)",
)
;(global as any).__BLITZ_SESSION_MIDDLEWARE_USED = true

global.sessionConfig = {
...defaultConfig,
...sessionConfig,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/auth/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ErrorBoundary} from "react-error-boundary"
import {ReactQueryDevtools} from "react-query/devtools"

if (typeof window !== "undefined") {
window["DEBUG_BLITZ"] = 1
;(window as any).DEBUG_BLITZ = 1
}

export default function App({Component, pageProps}: AppProps) {
Expand Down
27 changes: 26 additions & 1 deletion test/integration/auth/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
/* eslint-env jest */
import {findPort, killApp, launchApp, renderViaHTTP, waitFor} from "lib/blitz-test-utils"
import fs from "fs-extra"
import {
blitzBuild,
blitzExport,
findPort,
killApp,
launchApp,
renderViaHTTP,
waitFor,
} from "lib/blitz-test-utils"
import webdriver from "lib/next-webdriver"
import {join} from "path"
import rimraf from "rimraf"
Expand Down Expand Up @@ -107,3 +116,19 @@ describe("Auth", () => {
})
})
})

const appDir = join(__dirname, "../")
const outdir = join(appDir, "out")

describe("auth - blitz export should not work", () => {
it("should build successfully", async () => {
await fs.remove(join(appDir, ".next"))
const {code} = await blitzBuild(appDir)
if (code !== 0) throw new Error(`build failed with status ${code}`)
})

it("should have error during blitz export", async () => {
const {stderr} = await blitzExport(appDir, {outdir}, {stderr: true})
expect(stderr).toContain("Blitz sessionMiddleware is not compatible with `blitz export`.")
})
})
4 changes: 4 additions & 0 deletions test/integration/export-image-default/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: ["blitz/babel"],
plugins: [],
}
1 change: 1 addition & 0 deletions test/integration/export-image-default/blitz.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
7 changes: 7 additions & 0 deletions test/integration/export-image-default/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function App({Component, pageProps}) {
return (
<>
<Component {...pageProps} />
</>
)
}
8 changes: 8 additions & 0 deletions test/integration/export-image-default/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {Image} from "blitz"

export default () => (
<div>
<p>Should error during export</p>
<Image src="/i.png" width="10" height="10" />
</div>
)
24 changes: 24 additions & 0 deletions test/integration/export-image-default/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-env jest */

import fs from "fs-extra"
import {blitzBuild, blitzExport} from "lib/blitz-test-utils"
import {join} from "path"

jest.setTimeout(1000 * 60 * 5)
const appDir = join(__dirname, "../")
const outdir = join(appDir, "out")

describe("Export with default loader image component", () => {
it("should build successfully", async () => {
await fs.remove(join(appDir, ".next"))
const {code} = await blitzBuild(appDir)
if (code !== 0) throw new Error(`build failed with status ${code}`)
})

it("should have error during blitzx export", async () => {
const {stderr} = await blitzExport(appDir, {outdir}, {stderr: true})
expect(stderr).toContain(
"Image Optimization using Next.js' default loader is not compatible with `next export`.",
)
})
})
26 changes: 25 additions & 1 deletion test/integration/misc/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/* eslint-env jest */
import {findPort, killApp, launchApp, renderViaHTTP} from "lib/blitz-test-utils"
import fs from "fs-extra"
import {
blitzBuild,
blitzExport,
findPort,
killApp,
launchApp,
renderViaHTTP,
} from "lib/blitz-test-utils"
import webdriver from "lib/next-webdriver"
import {join} from "path"

Expand Down Expand Up @@ -29,4 +37,20 @@ describe("Misc", () => {
if (browser) await browser.close()
})
})

const appDir = join(__dirname, "../")
const outdir = join(appDir, "out")

describe("blitz export", () => {
it("should build successfully", async () => {
await fs.remove(join(appDir, ".next"))
const {code} = await blitzBuild(appDir)
if (code !== 0) throw new Error(`build failed with status ${code}`)
})

it("should export successfully", async () => {
const {code} = await blitzExport(appDir, {outdir})
if (code !== 0) throw new Error(`export failed with status ${code}`)
})
})
})
6 changes: 3 additions & 3 deletions test/lib/blitz-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function runBlitzCommand(argv: any[], options: RunBlitzCommandOptions = {
__NEXT_TEST_MODE: "true",
}

return new Promise((resolve, reject) => {
return new Promise<any>((resolve, reject) => {
console.log(`Running command "blitz ${argv.join(" ")}"`)
const instance = spawn("node", ["--no-deprecation", blitzBin, ...argv], {
...options.spawnOptions,
Expand All @@ -141,14 +141,14 @@ export function runBlitzCommand(argv: any[], options: RunBlitzCommandOptions = {

let stderrOutput = ""
if (options.stderr) {
instance.stderr.on("data", function (chunk) {
instance.stderr?.on("data", function (chunk) {
stderrOutput += chunk
})
}

let stdoutOutput = ""
if (options.stdout) {
instance.stdout.on("data", function (chunk) {
instance.stdout?.on("data", function (chunk) {
stdoutOutput += chunk
})
}
Expand Down