Compare commits
61 Commits
@blitzjs/a
...
@blitzjs/c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e476beba39 | ||
|
|
1a1b23a7e8 | ||
|
|
ae0b714f69 | ||
|
|
7817fe3a85 | ||
|
|
30bb474abb | ||
|
|
135b30efde | ||
|
|
527e48ac3e | ||
|
|
b905270875 | ||
|
|
96ea5291e4 | ||
|
|
9c2e7d372c | ||
|
|
493d505b24 | ||
|
|
09da992bef | ||
|
|
bbac7906e8 | ||
|
|
21ca3a9b02 | ||
|
|
32274803d9 | ||
|
|
9ded8dacba | ||
|
|
80ffbeaa4c | ||
|
|
6bde1b07da | ||
|
|
b918055bf3 | ||
|
|
f9a2971f05 | ||
|
|
72b08f2269 | ||
|
|
2124a4d0c5 | ||
|
|
8aee25c58a | ||
|
|
f1003faf94 | ||
|
|
50468a3bb0 | ||
|
|
891d91bf4d | ||
|
|
f96c953457 | ||
|
|
a80d2a8f77 | ||
|
|
b336ad05f4 | ||
|
|
39ca0ef8bf | ||
|
|
4cad9cca25 | ||
|
|
b6fc940bf2 | ||
|
|
a946dd5889 | ||
|
|
e3750b049d | ||
|
|
fb01cc7788 | ||
|
|
ac8c412da2 | ||
|
|
dfd2408e95 | ||
|
|
9741287050 | ||
|
|
0e9c81abdc | ||
|
|
9e05d6e155 | ||
|
|
17f70e65ef | ||
|
|
0ddc5a8169 | ||
|
|
e6fb09d494 | ||
|
|
d846fc6be9 | ||
|
|
f5e80e3835 | ||
|
|
17ce29e5e4 | ||
|
|
46d9f81adf | ||
|
|
994cfc6292 | ||
|
|
7811748526 | ||
|
|
ce45368334 | ||
|
|
4e9c1f60b6 | ||
|
|
508682c8f8 | ||
|
|
962eb58af6 | ||
|
|
17669b3af8 | ||
|
|
ec6299c36a | ||
|
|
6ac2d3412a | ||
|
|
85f9959d1f | ||
|
|
354f0440d6 | ||
|
|
ac365a0656 | ||
|
|
0729291099 | ||
|
|
9cf924ee86 |
5
.changeset/bright-mangos-run.md
Normal file
5
.changeset/bright-mangos-run.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/rpc": patch
|
||||
---
|
||||
|
||||
Add queryClient to RPC Plugin exports
|
||||
5
.changeset/cool-doors-invent.md
Normal file
5
.changeset/cool-doors-invent.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/rpc": patch
|
||||
---
|
||||
|
||||
Add invokeWithCtx function
|
||||
6
.changeset/eleven-humans-sort.md
Normal file
6
.changeset/eleven-humans-sort.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@blitzjs/codemod": patch
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
codemod fixes
|
||||
5
.changeset/empty-turkeys-wave.md
Normal file
5
.changeset/empty-turkeys-wave.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/next": patch
|
||||
---
|
||||
|
||||
Use `useRouter` from next/router in useParams function
|
||||
5
.changeset/famous-kings-explain.md
Normal file
5
.changeset/famous-kings-explain.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
updated nextjs version in generator & npmrc file
|
||||
6
.changeset/four-brooms-juggle.md
Normal file
6
.changeset/four-brooms-juggle.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@blitzjs/auth": patch
|
||||
"@blitzjs/next": patch
|
||||
---
|
||||
|
||||
Add missing RouteUrlObject on Page.authenticate.redirectTo
|
||||
5
.changeset/fuzzy-jars-admire.md
Normal file
5
.changeset/fuzzy-jars-admire.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/codemod": patch
|
||||
---
|
||||
|
||||
fix codemod for wrapping \_app arrow function & fix codemod for nested pages directory
|
||||
5
.changeset/gentle-dogs-reply.md
Normal file
5
.changeset/gentle-dogs-reply.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/codemod": patch
|
||||
---
|
||||
|
||||
Update queryClient import in codemod
|
||||
5
.changeset/good-insects-wink.md
Normal file
5
.changeset/good-insects-wink.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/next": patch
|
||||
---
|
||||
|
||||
Allow passing optional type argument for ParamsType in GSSP
|
||||
5
.changeset/green-papayas-do.md
Normal file
5
.changeset/green-papayas-do.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
Update codemod and template with a new queryClient import location
|
||||
5
.changeset/healthy-rice-shout.md
Normal file
5
.changeset/healthy-rice-shout.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
detailed print env info
|
||||
7
.changeset/lucky-cows-try.md
Normal file
7
.changeset/lucky-cows-try.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"blitz": patch
|
||||
"@blitzjs/auth": patch
|
||||
"@blitzjs/next": patch
|
||||
---
|
||||
|
||||
rename middleware type for blitz server plugin
|
||||
5
.changeset/nervous-beds-travel.md
Normal file
5
.changeset/nervous-beds-travel.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/next": patch
|
||||
---
|
||||
|
||||
useParam & useParams functions now accessible from @blitzjs/next
|
||||
5
.changeset/olive-bees-buy.md
Normal file
5
.changeset/olive-bees-buy.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/codemod": patch
|
||||
---
|
||||
|
||||
Fix templates source in RPC codemod step
|
||||
5
.changeset/olive-feet-rhyme.md
Normal file
5
.changeset/olive-feet-rhyme.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/codemod": patch
|
||||
---
|
||||
|
||||
Add codemod to upgrade from legacy framework to the Blitz Toolkit
|
||||
@@ -12,44 +12,70 @@
|
||||
"@blitzjs/rpc": "2.0.0-alpha.0",
|
||||
"@blitzjs/config": "0.0.0",
|
||||
"@blitzjs/generator": "2.0.0-alpha.0",
|
||||
"@blitzjs/codemod": "2.0.0-alpha.0",
|
||||
"template": "0.0.0",
|
||||
"toolkit-app": "1.0.0",
|
||||
"test-qm": "0.0.0"
|
||||
"test-qm": "0.0.0",
|
||||
"test-no-suspense": "0.0.0",
|
||||
"test-trailing-slash": "0.0.0"
|
||||
},
|
||||
"changesets": [
|
||||
"big-phones-bow",
|
||||
"breezy-cameras-double",
|
||||
"bright-mangos-run",
|
||||
"cool-doors-invent",
|
||||
"dirty-monkeys-greet",
|
||||
"eleven-humans-sort",
|
||||
"empty-berries-rule",
|
||||
"empty-turkeys-wave",
|
||||
"fair-wombats-sneeze",
|
||||
"famous-kings-explain",
|
||||
"fast-trainers-kneel",
|
||||
"flat-bees-approve",
|
||||
"four-brooms-juggle",
|
||||
"four-meals-fry",
|
||||
"fuzzy-jars-admire",
|
||||
"gentle-dogs-reply",
|
||||
"good-insects-wink",
|
||||
"great-months-train",
|
||||
"green-papayas-do",
|
||||
"healthy-rice-shout",
|
||||
"hot-drinks-approve",
|
||||
"lovely-colts-share",
|
||||
"lucky-cows-try",
|
||||
"modern-cameras-pull",
|
||||
"moody-squids-cheer",
|
||||
"nervous-beds-travel",
|
||||
"nice-starfishes-live",
|
||||
"nine-onions-admire",
|
||||
"ninety-pets-heal",
|
||||
"olive-bees-buy",
|
||||
"olive-feet-rhyme",
|
||||
"plenty-bottles-swim",
|
||||
"poor-peas-lick",
|
||||
"poor-penguins-look",
|
||||
"poor-shrimps-think",
|
||||
"purple-singers-greet",
|
||||
"quiet-feet-travel",
|
||||
"rich-chairs-invent",
|
||||
"sharp-falcons-begin",
|
||||
"shy-olives-hang",
|
||||
"silent-colts-reply",
|
||||
"small-socks-confess",
|
||||
"stupid-walls-sell",
|
||||
"swift-drinks-dress",
|
||||
"tame-keys-reply",
|
||||
"tasty-news-collect",
|
||||
"ten-rivers-burn",
|
||||
"tender-pianos-check",
|
||||
"thick-parrots-float",
|
||||
"thirty-countries-build",
|
||||
"twenty-beans-pump",
|
||||
"two-kiwis-help",
|
||||
"two-tigers-type",
|
||||
"unlucky-papayas-sleep",
|
||||
"violet-bags-leave",
|
||||
"violet-lions-help",
|
||||
"weak-suns-shave",
|
||||
"wicked-ghosts-cough",
|
||||
"wise-frogs-give"
|
||||
|
||||
7
.changeset/purple-singers-greet.md
Normal file
7
.changeset/purple-singers-greet.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"@blitzjs/rpc": patch
|
||||
"@blitzjs/codemod": patch
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
getQueryClient function & queryClient codemod updates & shared plugin config
|
||||
6
.changeset/shy-olives-hang.md
Normal file
6
.changeset/shy-olives-hang.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@blitzjs/rpc": patch
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
Update RPC plugin setup in templates
|
||||
5
.changeset/tame-keys-reply.md
Normal file
5
.changeset/tame-keys-reply.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
Add aliases for Blitz CLI commands
|
||||
6
.changeset/tasty-news-collect.md
Normal file
6
.changeset/tasty-news-collect.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"blitz": patch
|
||||
"@blitzjs/codemod": patch
|
||||
---
|
||||
|
||||
init codemod generator
|
||||
5
.changeset/thick-parrots-float.md
Normal file
5
.changeset/thick-parrots-float.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/codemod": patch
|
||||
---
|
||||
|
||||
allow extension catch in getAllFiles codemod util
|
||||
5
.changeset/two-tigers-type.md
Normal file
5
.changeset/two-tigers-type.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
Fix running bin commands with Blitz CLI
|
||||
5
.changeset/violet-bags-leave.md
Normal file
5
.changeset/violet-bags-leave.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/codemod": patch
|
||||
---
|
||||
|
||||
Update templates directory for codemod
|
||||
5
.changeset/violet-lions-help.md
Normal file
5
.changeset/violet-lions-help.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/rpc": patch
|
||||
---
|
||||
|
||||
Add resolverBasePath to Blitz config to change the way rpc routes are generated
|
||||
1
.npmrc
1
.npmrc
@@ -6,3 +6,4 @@ public-hoist-pattern[]=*types*
|
||||
public-hoist-pattern[]=*eslint*
|
||||
public-hoist-pattern[]=@prettier/plugin-*
|
||||
public-hoist-pattern[]=*prettier-plugin-*
|
||||
strict-peer-dependencies=false
|
||||
|
||||
@@ -7,12 +7,6 @@ export const { withBlitz } = setupBlitzClient({
|
||||
AuthClientPlugin({
|
||||
cookiePrefix: "web-cookie-prefix",
|
||||
}),
|
||||
BlitzRpcPlugin({
|
||||
reactQueryOptions: {
|
||||
queries: {
|
||||
staleTime: 7000,
|
||||
},
|
||||
},
|
||||
}),
|
||||
BlitzRpcPlugin({}),
|
||||
],
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"version": "1.0.1-alpha.16",
|
||||
"scripts": {
|
||||
"start:dev": "pnpm run prisma:start && next dev",
|
||||
"buildapp": "pnpm i && pnpm prisma generate && next build",
|
||||
"buildapp": "NODE_ENV=production pnpm blitz codegen && pnpm prisma generate && next build",
|
||||
"start": "next start",
|
||||
"lint": "next lint",
|
||||
"prisma:start": "prisma generate && prisma migrate deploy",
|
||||
@@ -29,7 +29,7 @@
|
||||
"@blitzjs/rpc": "workspace:*",
|
||||
"@hookform/resolvers": "2.8.8",
|
||||
"@prisma/client": "3.9.0",
|
||||
"blitz": "workspace:2.0.0-alpha.26",
|
||||
"blitz": "workspace:2.0.0-alpha.44",
|
||||
"next": "12.1.6-canary.17",
|
||||
"prisma": "3.9.0",
|
||||
"react": "18.0.0",
|
||||
|
||||
8
apps/web/app/queries/getUsersAuth.ts
Normal file
8
apps/web/app/queries/getUsersAuth.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import {resolver} from "@blitzjs/rpc"
|
||||
import db from "db"
|
||||
|
||||
export default resolver.pipe(resolver.authorize(), async () => {
|
||||
const users = await db.user.findMany()
|
||||
|
||||
return users
|
||||
})
|
||||
@@ -10,6 +10,7 @@ module.exports = withBlitz(
|
||||
customServer: {
|
||||
hotReload: false,
|
||||
},
|
||||
resolverBasePath: "root",
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -25,6 +25,7 @@ export default api(async (req, res, ctx) => {
|
||||
|
||||
await blitzContext.session.$create({
|
||||
userId: user.id,
|
||||
role: "USER",
|
||||
})
|
||||
|
||||
res.status(200).json({email: req.query.email, userId: blitzContext.session.userId})
|
||||
|
||||
@@ -16,6 +16,7 @@ export default api(async (req, res, ctx) => {
|
||||
|
||||
await blitzContext.session.$create({
|
||||
userId: user.id,
|
||||
role: "USER",
|
||||
})
|
||||
|
||||
res.status(200).json({userId: blitzContext.session.userId, ...user, email: req.query.email})
|
||||
|
||||
@@ -9,7 +9,7 @@ export const getServerSideProps = gSSP(async ({ctx}) => {
|
||||
return {props: {}}
|
||||
})
|
||||
|
||||
function PageWithGssp(props) {
|
||||
function PageWithPrefetchInfiniteQuery(props) {
|
||||
const [usersPages] = useInfiniteQuery(getInfiniteUsers, (page = {take: 3, skip: 0}) => page, {
|
||||
getNextPageParam: (lastPage) => lastPage.nextPage,
|
||||
})
|
||||
@@ -29,4 +29,4 @@ function PageWithGssp(props) {
|
||||
)
|
||||
}
|
||||
|
||||
export default PageWithGssp
|
||||
export default PageWithPrefetchInfiniteQuery
|
||||
|
||||
31
apps/web/pages/page-with-invoke-ctx.tsx
Normal file
31
apps/web/pages/page-with-invoke-ctx.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import {SessionContext} from "@blitzjs/auth"
|
||||
import {invokeWithCtx} from "@blitzjs/rpc"
|
||||
import {gSSP} from "app/blitz-server"
|
||||
import getUsersAuth from "app/queries/getUsersAuth"
|
||||
|
||||
type Props = {
|
||||
userId: unknown
|
||||
publicData: SessionContext["$publicData"]
|
||||
}
|
||||
|
||||
export const getServerSideProps = gSSP<Props>(async ({ctx}) => {
|
||||
const {session} = ctx
|
||||
|
||||
const users = await invokeWithCtx(getUsersAuth, {}, ctx)
|
||||
|
||||
console.log({users})
|
||||
|
||||
return {
|
||||
props: {
|
||||
userId: session.userId,
|
||||
publicData: session.$publicData,
|
||||
publishedAt: new Date(0),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
function PageWithInvokeCtx(props: Props) {
|
||||
return <div>{JSON.stringify(props, null, 2)}</div>
|
||||
}
|
||||
|
||||
export default PageWithInvokeCtx
|
||||
@@ -9,7 +9,7 @@ export const getServerSideProps = gSSP(async ({ctx}) => {
|
||||
return {props: {}}
|
||||
})
|
||||
|
||||
function PageWithGssp(props) {
|
||||
function PageWithPrefetch(props) {
|
||||
const [users] = useQuery(getUsers, {})
|
||||
return (
|
||||
<div>
|
||||
@@ -25,4 +25,4 @@ function PageWithGssp(props) {
|
||||
)
|
||||
}
|
||||
|
||||
export default PageWithGssp
|
||||
export default PageWithPrefetch
|
||||
|
||||
15
apps/web/types.ts
Normal file
15
apps/web/types.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import {SimpleRolesIsAuthorized} from "@blitzjs/auth"
|
||||
import {User} from "db"
|
||||
|
||||
export type Role = "ADMIN" | "USER"
|
||||
|
||||
declare module "@blitzjs/auth" {
|
||||
export interface Session {
|
||||
isAuthorized: SimpleRolesIsAuthorized<Role>
|
||||
PublicData: {
|
||||
userId: User["id"]
|
||||
role: Role
|
||||
views?: number
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import {withBlitz} from "../app/blitz-client"
|
||||
|
||||
function RootErrorFallback({error}: ErrorFallbackProps) {
|
||||
if (error instanceof AuthenticationError) {
|
||||
return <div>Error: You are not authenticated</div>
|
||||
return <div id="error">Error: You are not authenticated</div>
|
||||
} else if (error instanceof AuthorizationError) {
|
||||
return (
|
||||
<ErrorComponent
|
||||
|
||||
@@ -35,20 +35,24 @@ const runTests = (mode?: string) => {
|
||||
"should render error for protected query",
|
||||
async () => {
|
||||
const browser = await webdriver(appPort, "/authenticated-page")
|
||||
let errorMsg = await browser.elementByXpath(`//*[@id="__next"]/div`)
|
||||
let errorMsg = await browser.elementById(`error`).text()
|
||||
expect(errorMsg).toMatch(/Error: You are not authenticated/)
|
||||
if (browser) browser.close()
|
||||
},
|
||||
5000 * 60 * 2,
|
||||
)
|
||||
|
||||
it("should render result for open query", async () => {
|
||||
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
|
||||
method: "GET",
|
||||
headers: {"Content-Type": "application/json; charset=utf-8"},
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
})
|
||||
it(
|
||||
"should render result for open query",
|
||||
async () => {
|
||||
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
|
||||
method: "GET",
|
||||
headers: {"Content-Type": "application/json; charset=utf-8"},
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
},
|
||||
5000 * 60 * 2,
|
||||
)
|
||||
|
||||
it("sets correct cookie", async () => {
|
||||
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
|
||||
@@ -118,31 +122,33 @@ const runTests = (mode?: string) => {
|
||||
})
|
||||
}
|
||||
|
||||
describe("dev mode", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
appPort = await findPort()
|
||||
app = await launchApp(appDir, appPort, {})
|
||||
await seed()
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}, 5000 * 60 * 2)
|
||||
afterAll(async () => await killApp(app))
|
||||
runTests()
|
||||
})
|
||||
describe("Auth Tests", () => {
|
||||
describe("dev mode", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
appPort = await findPort()
|
||||
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
|
||||
await seed()
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}, 5000 * 60 * 2)
|
||||
afterAll(async () => await killApp(app))
|
||||
runTests()
|
||||
})
|
||||
|
||||
describe("server mode", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
appPort = await findPort()
|
||||
await nextBuild(appDir)
|
||||
app = await nextStart(appDir, appPort)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
}, 5000 * 60 * 2)
|
||||
afterAll(async () => await killApp(app))
|
||||
describe("server mode", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
await nextBuild(appDir)
|
||||
appPort = await findPort()
|
||||
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
}, 5000 * 60 * 2)
|
||||
afterAll(async () => await killApp(app))
|
||||
|
||||
runTests()
|
||||
runTests()
|
||||
})
|
||||
})
|
||||
|
||||
2
integration-tests/no-suspense/.env
Normal file
2
integration-tests/no-suspense/.env
Normal file
@@ -0,0 +1,2 @@
|
||||
SESSION_SECRET_KEY=hsdenhJfpLHrGjgdgg3jdF8g2bYD2PaQ
|
||||
HEADLESS=true
|
||||
1
integration-tests/no-suspense/.eslintrc.js
Normal file
1
integration-tests/no-suspense/.eslintrc.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("@blitzjs/config/eslint")
|
||||
3
integration-tests/no-suspense/.gitignore
vendored
Normal file
3
integration-tests/no-suspense/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
# Keep environment variables out of version control
|
||||
*.sqlite
|
||||
14
integration-tests/no-suspense/app/blitz-client.ts
Normal file
14
integration-tests/no-suspense/app/blitz-client.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import {BlitzRpcPlugin} from "@blitzjs/rpc"
|
||||
import {setupBlitzClient} from "@blitzjs/next"
|
||||
import {AuthClientPlugin} from "@blitzjs/auth"
|
||||
|
||||
const {withBlitz} = setupBlitzClient({
|
||||
plugins: [
|
||||
AuthClientPlugin({
|
||||
cookiePrefix: "no-suspense-tests-cookie-prefix",
|
||||
}),
|
||||
BlitzRpcPlugin({}),
|
||||
],
|
||||
})
|
||||
|
||||
export {withBlitz}
|
||||
16
integration-tests/no-suspense/app/blitz-server.ts
Normal file
16
integration-tests/no-suspense/app/blitz-server.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import {setupBlitzServer} from "@blitzjs/next"
|
||||
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
|
||||
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
|
||||
import {prisma as db} from "../prisma/index"
|
||||
|
||||
const {gSSP, gSP, api} = setupBlitzServer({
|
||||
plugins: [
|
||||
AuthServerPlugin({
|
||||
cookiePrefix: "no-suspense-tests-cookie-prefix",
|
||||
storage: PrismaStorage(db as any),
|
||||
isAuthorized: simpleRolesIsAuthorized,
|
||||
}),
|
||||
],
|
||||
})
|
||||
|
||||
export {gSSP, gSP, api}
|
||||
3
integration-tests/no-suspense/app/queries/getBasic.ts
Normal file
3
integration-tests/no-suspense/app/queries/getBasic.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default async function getBasic() {
|
||||
return "basic-result"
|
||||
}
|
||||
5
integration-tests/no-suspense/next-env.d.ts
vendored
Normal file
5
integration-tests/no-suspense/next-env.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/basic-features/typescript for more information.
|
||||
2
integration-tests/no-suspense/next.config.js
Normal file
2
integration-tests/no-suspense/next.config.js
Normal file
@@ -0,0 +1,2 @@
|
||||
const {withBlitz} = require("@blitzjs/next")
|
||||
module.exports = withBlitz({})
|
||||
41
integration-tests/no-suspense/package.json
Normal file
41
integration-tests/no-suspense/package.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "test-no-suspense",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start:dev": "pnpm run prisma:start && next dev",
|
||||
"test": "pnpm run prisma:start && vitest run",
|
||||
"test-watch": "vitest",
|
||||
"start": "next start",
|
||||
"lint": "next lint",
|
||||
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf .next",
|
||||
"prisma:start": "prisma generate && prisma migrate deploy",
|
||||
"prisma:studio": "prisma studio"
|
||||
},
|
||||
"dependencies": {
|
||||
"@blitzjs/auth": "workspace:*",
|
||||
"@blitzjs/next": "workspace:*",
|
||||
"@blitzjs/rpc": "workspace:*",
|
||||
"@prisma/client": "3.9.0",
|
||||
"blitz": "workspace:*",
|
||||
"lowdb": "3.0.0",
|
||||
"next": "12.1.6-canary.17",
|
||||
"prisma": "3.9.0",
|
||||
"react": "18.0.0",
|
||||
"react-dom": "18.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "workspace:*",
|
||||
"@next/bundle-analyzer": "12.0.8",
|
||||
"@types/express": "4.17.13",
|
||||
"@types/fs-extra": "9.0.13",
|
||||
"@types/node-fetch": "2.6.1",
|
||||
"@types/react": "18.0.1",
|
||||
"b64-lite": "1.4.0",
|
||||
"eslint": "7.32.0",
|
||||
"fs-extra": "10.0.1",
|
||||
"get-port": "6.1.2",
|
||||
"node-fetch": "3.2.3",
|
||||
"typescript": "^4.5.3"
|
||||
}
|
||||
}
|
||||
34
integration-tests/no-suspense/pages/_app.tsx
Normal file
34
integration-tests/no-suspense/pages/_app.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
|
||||
import {AuthenticationError, AuthorizationError} from "blitz"
|
||||
import React from "react"
|
||||
import {withBlitz} from "../app/blitz-client"
|
||||
|
||||
function RootErrorFallback({error}: ErrorFallbackProps) {
|
||||
if (error instanceof AuthenticationError) {
|
||||
return <div>Error: You are not authenticated</div>
|
||||
} else if (error instanceof AuthorizationError) {
|
||||
return (
|
||||
<ErrorComponent
|
||||
statusCode={error.statusCode}
|
||||
title="Sorry, you are not authorized to access this"
|
||||
/>
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
<ErrorComponent
|
||||
statusCode={(error as any)?.statusCode || 400}
|
||||
title={error.message || error.name}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function MyApp({Component, pageProps}: AppProps) {
|
||||
return (
|
||||
<ErrorBoundary FallbackComponent={RootErrorFallback}>
|
||||
<Component {...pageProps} />
|
||||
</ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
export default withBlitz(MyApp)
|
||||
@@ -0,0 +1,4 @@
|
||||
import {rpcHandler} from "@blitzjs/rpc"
|
||||
import {api} from "../../../app/blitz-server"
|
||||
|
||||
export default api(rpcHandler({onError: console.log}))
|
||||
23
integration-tests/no-suspense/pages/use-query.tsx
Normal file
23
integration-tests/no-suspense/pages/use-query.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
import getBasic from "../app/queries/getBasic"
|
||||
import {useQuery} from "@blitzjs/rpc"
|
||||
import React from "react"
|
||||
|
||||
function Content() {
|
||||
const [result, {isFetching}] = useQuery(getBasic, undefined)
|
||||
|
||||
if (isFetching) {
|
||||
return <>Loading...</>
|
||||
} else {
|
||||
return <div id="content">{result}</div>
|
||||
}
|
||||
}
|
||||
|
||||
function Page() {
|
||||
return (
|
||||
<div id="page">
|
||||
<Content />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Page
|
||||
8
integration-tests/no-suspense/prisma/index.ts
Normal file
8
integration-tests/no-suspense/prisma/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import {enhancePrisma} from "blitz"
|
||||
import {PrismaClient} from "@prisma/client"
|
||||
|
||||
const EnhancedPrisma = enhancePrisma(PrismaClient)
|
||||
|
||||
export * from "@prisma/client"
|
||||
const prisma = new EnhancedPrisma()
|
||||
export {prisma}
|
||||
@@ -0,0 +1,47 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" DATETIME NOT NULL,
|
||||
"name" TEXT,
|
||||
"email" TEXT NOT NULL,
|
||||
"hashedPassword" TEXT,
|
||||
"role" TEXT NOT NULL DEFAULT 'user'
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Session" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" DATETIME NOT NULL,
|
||||
"expiresAt" DATETIME,
|
||||
"handle" TEXT NOT NULL,
|
||||
"userId" INTEGER,
|
||||
"hashedSessionToken" TEXT,
|
||||
"antiCSRFToken" TEXT,
|
||||
"publicData" TEXT,
|
||||
"privateData" TEXT,
|
||||
CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Token" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" DATETIME NOT NULL,
|
||||
"hashedToken" TEXT NOT NULL,
|
||||
"type" TEXT NOT NULL,
|
||||
"expiresAt" DATETIME NOT NULL,
|
||||
"sentTo" TEXT NOT NULL,
|
||||
"userId" INTEGER NOT NULL,
|
||||
CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Session_handle_key" ON "Session"("handle");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type");
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "sqlite"
|
||||
50
integration-tests/no-suspense/prisma/schema.prisma
Normal file
50
integration-tests/no-suspense/prisma/schema.prisma
Normal file
@@ -0,0 +1,50 @@
|
||||
datasource sqlite {
|
||||
provider = "sqlite"
|
||||
url = "file:./db.sqlite"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
name String?
|
||||
email String @unique
|
||||
hashedPassword String?
|
||||
role String @default("user")
|
||||
|
||||
sessions Session[]
|
||||
tokens Token[]
|
||||
}
|
||||
|
||||
model Session {
|
||||
id Int @id @default(autoincrement())
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
expiresAt DateTime?
|
||||
handle String @unique
|
||||
user User? @relation(fields: [userId], references: [id])
|
||||
userId Int?
|
||||
hashedSessionToken String?
|
||||
antiCSRFToken String?
|
||||
publicData String?
|
||||
privateData String?
|
||||
}
|
||||
|
||||
model Token {
|
||||
id Int @id @default(autoincrement())
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
hashedToken String
|
||||
type String
|
||||
expiresAt DateTime
|
||||
sentTo String
|
||||
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
userId Int
|
||||
|
||||
@@unique([hashedToken, type])
|
||||
}
|
||||
7
integration-tests/no-suspense/prisma/seed.ts
Normal file
7
integration-tests/no-suspense/prisma/seed.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import {prisma} from "./index"
|
||||
|
||||
const seed = async () => {
|
||||
await prisma.$reset()
|
||||
}
|
||||
|
||||
export default seed
|
||||
57
integration-tests/no-suspense/test/index.test.ts
Normal file
57
integration-tests/no-suspense/test/index.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import {describe, it, expect, beforeAll, afterAll} from "vitest"
|
||||
import {killApp, findPort, launchApp, nextBuild, nextStart} from "../../utils/next-test-utils"
|
||||
import webdriver from "../../utils/next-webdriver"
|
||||
|
||||
import {join} from "path"
|
||||
|
||||
let app: any
|
||||
let appPort: number
|
||||
const appDir = join(__dirname, "../")
|
||||
|
||||
const runTests = (mode?: string) => {
|
||||
describe("useQuery without suspense", () => {
|
||||
it(
|
||||
"should render query result",
|
||||
async () => {
|
||||
const browser = await webdriver(appPort, "/use-query")
|
||||
|
||||
let text
|
||||
browser.waitForElementByCss("#content", 0)
|
||||
text = await browser.elementByCss("#content").text()
|
||||
expect(text).toMatch(/basic-result/)
|
||||
if (browser) await browser.close()
|
||||
},
|
||||
5000 * 60 * 2,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
describe("No Suspense Tests", () => {
|
||||
describe("dev mode", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
appPort = await findPort()
|
||||
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}, 5000 * 60 * 2)
|
||||
afterAll(async () => await killApp(app))
|
||||
runTests()
|
||||
})
|
||||
|
||||
describe("server mode", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
await nextBuild(appDir)
|
||||
appPort = await findPort()
|
||||
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
}, 5000 * 60 * 2)
|
||||
afterAll(async () => await killApp(app))
|
||||
|
||||
runTests()
|
||||
})
|
||||
})
|
||||
11
integration-tests/no-suspense/tsconfig.json
Normal file
11
integration-tests/no-suspense/tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "@blitzjs/config/tsconfig.nextjs.json",
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
|
||||
"compilerOptions": {
|
||||
"paths": {
|
||||
"react": ["./node_modules/@types/react"]
|
||||
}
|
||||
},
|
||||
"exclude": ["node_modules"],
|
||||
"baseUrl": "."
|
||||
}
|
||||
@@ -143,7 +143,7 @@ describe("RPC", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
appPort = await findPort()
|
||||
app = await launchApp(appDir, appPort)
|
||||
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
@@ -162,7 +162,7 @@ describe("RPC", () => {
|
||||
await nextBuild(appDir)
|
||||
mode = "server"
|
||||
appPort = await findPort()
|
||||
app = await nextStart(appDir, appPort)
|
||||
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
|
||||
})
|
||||
afterAll(() => killApp(app))
|
||||
|
||||
@@ -185,7 +185,7 @@ describe("RPC", () => {
|
||||
await nextBuild(appDir)
|
||||
mode = "serverless"
|
||||
appPort = await findPort()
|
||||
app = await nextStart(appDir, appPort)
|
||||
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
|
||||
})
|
||||
afterAll(async () => {
|
||||
await killApp(app)
|
||||
|
||||
2
integration-tests/trailing-slash/.env
Normal file
2
integration-tests/trailing-slash/.env
Normal file
@@ -0,0 +1,2 @@
|
||||
SESSION_SECRET_KEY=hsdenhJfpLHrGjgdgg3jdF8g2bYD2PaQ
|
||||
HEADLESS=true
|
||||
1
integration-tests/trailing-slash/.eslintrc.js
Normal file
1
integration-tests/trailing-slash/.eslintrc.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("@blitzjs/config/eslint")
|
||||
3
integration-tests/trailing-slash/.gitignore
vendored
Normal file
3
integration-tests/trailing-slash/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
# Keep environment variables out of version control
|
||||
*.sqlite
|
||||
14
integration-tests/trailing-slash/app/blitz-client.ts
Normal file
14
integration-tests/trailing-slash/app/blitz-client.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import {BlitzRpcPlugin} from "@blitzjs/rpc"
|
||||
import {setupBlitzClient} from "@blitzjs/next"
|
||||
import {AuthClientPlugin} from "@blitzjs/auth"
|
||||
|
||||
const {withBlitz} = setupBlitzClient({
|
||||
plugins: [
|
||||
AuthClientPlugin({
|
||||
cookiePrefix: "trailing-slash-tests-cookie-prefix",
|
||||
}),
|
||||
BlitzRpcPlugin({}),
|
||||
],
|
||||
})
|
||||
|
||||
export {withBlitz}
|
||||
16
integration-tests/trailing-slash/app/blitz-server.ts
Normal file
16
integration-tests/trailing-slash/app/blitz-server.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import {setupBlitzServer} from "@blitzjs/next"
|
||||
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
|
||||
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
|
||||
import {prisma as db} from "../prisma/index"
|
||||
|
||||
const {gSSP, gSP, api} = setupBlitzServer({
|
||||
plugins: [
|
||||
AuthServerPlugin({
|
||||
cookiePrefix: "trailing-slash-tests-cookie-prefix",
|
||||
storage: PrismaStorage(db as any),
|
||||
isAuthorized: simpleRolesIsAuthorized,
|
||||
}),
|
||||
],
|
||||
})
|
||||
|
||||
export {gSSP, gSP, api}
|
||||
3
integration-tests/trailing-slash/app/queries/getBasic.ts
Normal file
3
integration-tests/trailing-slash/app/queries/getBasic.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default async function getBasic() {
|
||||
return "basic-result"
|
||||
}
|
||||
5
integration-tests/trailing-slash/next-env.d.ts
vendored
Normal file
5
integration-tests/trailing-slash/next-env.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/basic-features/typescript for more information.
|
||||
4
integration-tests/trailing-slash/next.config.js
Normal file
4
integration-tests/trailing-slash/next.config.js
Normal file
@@ -0,0 +1,4 @@
|
||||
const {withBlitz} = require("@blitzjs/next")
|
||||
module.exports = withBlitz({
|
||||
trailingSlash: true,
|
||||
})
|
||||
41
integration-tests/trailing-slash/package.json
Normal file
41
integration-tests/trailing-slash/package.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "test-trailing-slash",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start:dev": "pnpm run prisma:start && next dev",
|
||||
"test": "pnpm run prisma:start && vitest run",
|
||||
"test-watch": "vitest",
|
||||
"start": "next start",
|
||||
"lint": "next lint",
|
||||
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf .next",
|
||||
"prisma:start": "prisma generate && prisma migrate deploy",
|
||||
"prisma:studio": "prisma studio"
|
||||
},
|
||||
"dependencies": {
|
||||
"@blitzjs/auth": "workspace:*",
|
||||
"@blitzjs/next": "workspace:*",
|
||||
"@blitzjs/rpc": "workspace:*",
|
||||
"@prisma/client": "3.9.0",
|
||||
"blitz": "workspace:*",
|
||||
"lowdb": "3.0.0",
|
||||
"next": "12.1.6-canary.17",
|
||||
"prisma": "3.9.0",
|
||||
"react": "18.0.0",
|
||||
"react-dom": "18.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "workspace:*",
|
||||
"@next/bundle-analyzer": "12.0.8",
|
||||
"@types/express": "4.17.13",
|
||||
"@types/fs-extra": "9.0.13",
|
||||
"@types/node-fetch": "2.6.1",
|
||||
"@types/react": "18.0.1",
|
||||
"b64-lite": "1.4.0",
|
||||
"eslint": "7.32.0",
|
||||
"fs-extra": "10.0.1",
|
||||
"get-port": "6.1.2",
|
||||
"node-fetch": "3.2.3",
|
||||
"typescript": "^4.5.3"
|
||||
}
|
||||
}
|
||||
34
integration-tests/trailing-slash/pages/_app.tsx
Normal file
34
integration-tests/trailing-slash/pages/_app.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
|
||||
import {AuthenticationError, AuthorizationError} from "blitz"
|
||||
import React from "react"
|
||||
import {withBlitz} from "../app/blitz-client"
|
||||
|
||||
function RootErrorFallback({error}: ErrorFallbackProps) {
|
||||
if (error instanceof AuthenticationError) {
|
||||
return <div>Error: You are not authenticated</div>
|
||||
} else if (error instanceof AuthorizationError) {
|
||||
return (
|
||||
<ErrorComponent
|
||||
statusCode={error.statusCode}
|
||||
title="Sorry, you are not authorized to access this"
|
||||
/>
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
<ErrorComponent
|
||||
statusCode={(error as any)?.statusCode || 400}
|
||||
title={error.message || error.name}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function MyApp({Component, pageProps}: AppProps) {
|
||||
return (
|
||||
<ErrorBoundary FallbackComponent={RootErrorFallback}>
|
||||
<Component {...pageProps} />
|
||||
</ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
export default withBlitz(MyApp)
|
||||
@@ -0,0 +1,4 @@
|
||||
import {rpcHandler} from "@blitzjs/rpc"
|
||||
import {api} from "../../../app/blitz-server"
|
||||
|
||||
export default api(rpcHandler({onError: console.log}))
|
||||
20
integration-tests/trailing-slash/pages/use-query.tsx
Normal file
20
integration-tests/trailing-slash/pages/use-query.tsx
Normal file
@@ -0,0 +1,20 @@
|
||||
import getBasic from "../app/queries/getBasic"
|
||||
import {useQuery} from "@blitzjs/rpc"
|
||||
import {Suspense} from "react"
|
||||
|
||||
function Content() {
|
||||
const [result] = useQuery(getBasic, undefined)
|
||||
return <div id="content">{result}</div>
|
||||
}
|
||||
|
||||
function Page() {
|
||||
return (
|
||||
<div id="page">
|
||||
<Suspense fallback={"Loading..."}>
|
||||
<Content />
|
||||
</Suspense>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Page
|
||||
8
integration-tests/trailing-slash/prisma/index.ts
Normal file
8
integration-tests/trailing-slash/prisma/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import {enhancePrisma} from "blitz"
|
||||
import {PrismaClient} from "@prisma/client"
|
||||
|
||||
const EnhancedPrisma = enhancePrisma(PrismaClient)
|
||||
|
||||
export * from "@prisma/client"
|
||||
const prisma = new EnhancedPrisma()
|
||||
export {prisma}
|
||||
@@ -0,0 +1,47 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" DATETIME NOT NULL,
|
||||
"name" TEXT,
|
||||
"email" TEXT NOT NULL,
|
||||
"hashedPassword" TEXT,
|
||||
"role" TEXT NOT NULL DEFAULT 'user'
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Session" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" DATETIME NOT NULL,
|
||||
"expiresAt" DATETIME,
|
||||
"handle" TEXT NOT NULL,
|
||||
"userId" INTEGER,
|
||||
"hashedSessionToken" TEXT,
|
||||
"antiCSRFToken" TEXT,
|
||||
"publicData" TEXT,
|
||||
"privateData" TEXT,
|
||||
CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Token" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" DATETIME NOT NULL,
|
||||
"hashedToken" TEXT NOT NULL,
|
||||
"type" TEXT NOT NULL,
|
||||
"expiresAt" DATETIME NOT NULL,
|
||||
"sentTo" TEXT NOT NULL,
|
||||
"userId" INTEGER NOT NULL,
|
||||
CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Session_handle_key" ON "Session"("handle");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type");
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "sqlite"
|
||||
50
integration-tests/trailing-slash/prisma/schema.prisma
Normal file
50
integration-tests/trailing-slash/prisma/schema.prisma
Normal file
@@ -0,0 +1,50 @@
|
||||
datasource sqlite {
|
||||
provider = "sqlite"
|
||||
url = "file:./db.sqlite"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
name String?
|
||||
email String @unique
|
||||
hashedPassword String?
|
||||
role String @default("user")
|
||||
|
||||
sessions Session[]
|
||||
tokens Token[]
|
||||
}
|
||||
|
||||
model Session {
|
||||
id Int @id @default(autoincrement())
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
expiresAt DateTime?
|
||||
handle String @unique
|
||||
user User? @relation(fields: [userId], references: [id])
|
||||
userId Int?
|
||||
hashedSessionToken String?
|
||||
antiCSRFToken String?
|
||||
publicData String?
|
||||
privateData String?
|
||||
}
|
||||
|
||||
model Token {
|
||||
id Int @id @default(autoincrement())
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
hashedToken String
|
||||
type String
|
||||
expiresAt DateTime
|
||||
sentTo String
|
||||
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
userId Int
|
||||
|
||||
@@unique([hashedToken, type])
|
||||
}
|
||||
7
integration-tests/trailing-slash/prisma/seed.ts
Normal file
7
integration-tests/trailing-slash/prisma/seed.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import {prisma} from "./index"
|
||||
|
||||
const seed = async () => {
|
||||
await prisma.$reset()
|
||||
}
|
||||
|
||||
export default seed
|
||||
56
integration-tests/trailing-slash/test/index.test.ts
Normal file
56
integration-tests/trailing-slash/test/index.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import {describe, it, expect, beforeAll, afterAll} from "vitest"
|
||||
import {killApp, findPort, launchApp, nextBuild, nextStart} from "../../utils/next-test-utils"
|
||||
import webdriver from "../../utils/next-webdriver"
|
||||
|
||||
import {join} from "path"
|
||||
|
||||
let app: any
|
||||
let appPort: number
|
||||
const appDir = join(__dirname, "../")
|
||||
|
||||
const runTests = (mode?: string) => {
|
||||
describe("Trailing Slash", () => {
|
||||
it(
|
||||
"should render query result",
|
||||
async () => {
|
||||
const browser = await webdriver(appPort, "/use-query")
|
||||
let text = await browser.elementByCss("#page").text()
|
||||
expect(text).toMatch(/Loading/)
|
||||
await browser.waitForElementByCss("#content", 0)
|
||||
text = await browser.elementByCss("#content").text()
|
||||
expect(text).toMatch(/basic-result/)
|
||||
if (browser) await browser.close()
|
||||
},
|
||||
5000 * 60 * 2,
|
||||
)
|
||||
})
|
||||
}
|
||||
describe("Trailing Slash Tests", () => {
|
||||
describe("dev mode", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
appPort = await findPort()
|
||||
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}, 5000 * 60 * 2)
|
||||
afterAll(async () => await killApp(app))
|
||||
runTests()
|
||||
})
|
||||
|
||||
describe("server mode", () => {
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
await nextBuild(appDir)
|
||||
appPort = await findPort()
|
||||
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
}, 5000 * 60 * 2)
|
||||
afterAll(async () => await killApp(app))
|
||||
|
||||
runTests()
|
||||
})
|
||||
})
|
||||
11
integration-tests/trailing-slash/tsconfig.json
Normal file
11
integration-tests/trailing-slash/tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "@blitzjs/config/tsconfig.nextjs.json",
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
|
||||
"compilerOptions": {
|
||||
"paths": {
|
||||
"react": ["./node_modules/@types/react"]
|
||||
}
|
||||
},
|
||||
"exclude": ["node_modules"],
|
||||
"baseUrl": "."
|
||||
}
|
||||
@@ -80,21 +80,7 @@ const BlitzWrapper = ({plugins, children}) => {
|
||||
export function render(ui: RenderUI, {wrapper, router, ...options}: RenderOptions = {}) {
|
||||
if (!wrapper) {
|
||||
wrapper = ({children}) => {
|
||||
return (
|
||||
<BlitzWrapper
|
||||
plugins={[
|
||||
BlitzRpcPlugin({
|
||||
reactQueryOptions: {
|
||||
queries: {
|
||||
staleTime: 7000,
|
||||
},
|
||||
},
|
||||
}),
|
||||
]}
|
||||
>
|
||||
{children}
|
||||
</BlitzWrapper>
|
||||
)
|
||||
return <BlitzWrapper plugins={[BlitzRpcPlugin({})]}>{children}</BlitzWrapper>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
100
integration-tests/utils/browsers/base.ts
Normal file
100
integration-tests/utils/browsers/base.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
export type Event = "request"
|
||||
|
||||
// This is the base Browser interface all browser
|
||||
// classes should build off of, it is the bare
|
||||
// methods we aim to support across tests
|
||||
export class BrowserInterface {
|
||||
private promise: any
|
||||
private then: any
|
||||
private catch: any
|
||||
|
||||
protected chain(nextCall: any): BrowserInterface {
|
||||
if (!this.promise) {
|
||||
this.promise = Promise.resolve(this)
|
||||
}
|
||||
this.promise = this.promise.then(nextCall)
|
||||
this.then = (...args) => this.promise.then(...args)
|
||||
this.catch = (...args) => this.promise.catch(...args)
|
||||
return this
|
||||
}
|
||||
|
||||
async setup(browserName: string, locale?: string): Promise<void> {}
|
||||
async close(): Promise<void> {}
|
||||
async quit(): Promise<void> {}
|
||||
|
||||
elementsByCss(selector: string): BrowserInterface[] {
|
||||
return [this]
|
||||
}
|
||||
elementByCss(selector: string): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
elementById(selector: string): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
click(opts?: {modifierKey?: boolean}): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
keydown(key: string): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
keyup(key: string): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
focusPage(): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
type(text: string): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
waitForElementByCss(selector: string, timeout?: number): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
waitForCondition(snippet: string, timeout?: number): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
back(): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
forward(): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
refresh(): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
setDimensions(opts: {height: number; width: number}): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
addCookie(opts: {name: string; value: string}): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
deleteCookies(): BrowserInterface {
|
||||
return this
|
||||
}
|
||||
on(event: Event, cb: (...args: any[]) => void) {}
|
||||
off(event: Event, cb: (...args: any[]) => void) {}
|
||||
async loadPage(url: string, {disableCache: boolean, beforePageLoad: Function}): Promise<void> {}
|
||||
async get(url: string): Promise<void> {}
|
||||
|
||||
async getValue(): Promise<any> {}
|
||||
async getAttribute(name: string): Promise<any> {}
|
||||
async eval(snippet: string | Function): Promise<any> {}
|
||||
async evalAsync(snippet: string | Function): Promise<any> {}
|
||||
async text(): Promise<string> {
|
||||
return ""
|
||||
}
|
||||
async getComputedCss(prop: string): Promise<string> {
|
||||
return ""
|
||||
}
|
||||
async hasElementByCssSelector(selector: string): Promise<boolean> {
|
||||
return false
|
||||
}
|
||||
async log(): Promise<any[]> {
|
||||
return []
|
||||
}
|
||||
async websocketFrames(): Promise<any[]> {
|
||||
return []
|
||||
}
|
||||
async url(): Promise<string> {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
312
integration-tests/utils/browsers/playwright.ts
Normal file
312
integration-tests/utils/browsers/playwright.ts
Normal file
@@ -0,0 +1,312 @@
|
||||
import {BrowserInterface, Event} from "./base"
|
||||
import {
|
||||
chromium,
|
||||
webkit,
|
||||
firefox,
|
||||
Browser,
|
||||
BrowserContext,
|
||||
Page,
|
||||
ElementHandle,
|
||||
} from "playwright-chromium"
|
||||
|
||||
let page: Page
|
||||
let browser: Browser | undefined
|
||||
let context: BrowserContext | undefined
|
||||
let pageLogs: Array<{source: string; message: string}> = []
|
||||
let websocketFrames: Array<{payload: string | Buffer}> = []
|
||||
|
||||
export async function quit() {
|
||||
await context?.close()
|
||||
await browser?.close()
|
||||
context = undefined
|
||||
browser = undefined
|
||||
}
|
||||
|
||||
class Playwright extends BrowserInterface {
|
||||
private eventCallbacks: Record<Event, Set<(...args: any[]) => void>> = {
|
||||
request: new Set(),
|
||||
}
|
||||
|
||||
on(event: Event, cb: (...args: any[]) => void) {
|
||||
if (!this.eventCallbacks[event]) {
|
||||
throw new Error(
|
||||
`Invalid event passed to browser.on, received ${event}. Valid events are ${Object.keys(
|
||||
event,
|
||||
)}`,
|
||||
)
|
||||
}
|
||||
this.eventCallbacks[event]?.add(cb)
|
||||
}
|
||||
off(event: Event, cb: (...args: any[]) => void) {
|
||||
this.eventCallbacks[event]?.delete(cb)
|
||||
}
|
||||
|
||||
async setup(browserName: string, locale?: string) {
|
||||
if (browser) return
|
||||
const headless = !!process.env.HEADLESS || true
|
||||
|
||||
if (browserName === "safari") {
|
||||
browser = await webkit.launch({headless})
|
||||
} else if (browserName === "firefox") {
|
||||
browser = await firefox.launch({headless})
|
||||
} else {
|
||||
browser = await chromium.launch({headless, devtools: !headless})
|
||||
}
|
||||
context = await browser.newContext({locale})
|
||||
}
|
||||
|
||||
async get(url: string): Promise<void> {
|
||||
return page.goto(url) as any
|
||||
}
|
||||
|
||||
async loadPage(
|
||||
url: string,
|
||||
opts?: {disableCache: boolean; beforePageLoad?: (...args: any[]) => void},
|
||||
) {
|
||||
// clean-up existing pages
|
||||
for (const oldPage of context!.pages()) {
|
||||
await oldPage.close()
|
||||
}
|
||||
page = await context!.newPage()
|
||||
pageLogs = []
|
||||
websocketFrames = []
|
||||
|
||||
page.on("console", (msg) => {
|
||||
console.log("browser log:", msg)
|
||||
pageLogs.push({source: msg.type(), message: msg.text()})
|
||||
})
|
||||
page.on("crash", (page) => {
|
||||
console.error("page crashed")
|
||||
})
|
||||
page.on("pageerror", (error) => {
|
||||
console.error("page error", error)
|
||||
})
|
||||
page.on("request", (req) => {
|
||||
this.eventCallbacks.request.forEach((cb) => cb(req))
|
||||
})
|
||||
|
||||
if (opts?.disableCache) {
|
||||
// TODO: this doesn't seem to work (dev tools does not check the box as expected)
|
||||
const session = await context!.newCDPSession(page)
|
||||
session.send("Network.setCacheDisabled", {cacheDisabled: true})
|
||||
}
|
||||
|
||||
page.on("websocket", (ws) => {
|
||||
ws.on("framereceived", (frame) => {
|
||||
websocketFrames.push({payload: frame.payload})
|
||||
})
|
||||
})
|
||||
|
||||
opts?.beforePageLoad?.(page)
|
||||
|
||||
await page.goto(url, {waitUntil: "load"})
|
||||
}
|
||||
|
||||
back(): BrowserInterface {
|
||||
return this.chain(() => {
|
||||
return page.goBack()
|
||||
})
|
||||
}
|
||||
forward(): BrowserInterface {
|
||||
return this.chain(() => {
|
||||
return page.goForward()
|
||||
})
|
||||
}
|
||||
refresh(): BrowserInterface {
|
||||
return this.chain(() => {
|
||||
return page.reload()
|
||||
})
|
||||
}
|
||||
setDimensions({width, height}: {height: number; width: number}): BrowserInterface {
|
||||
return this.chain(() => page.setViewportSize({width, height}))
|
||||
}
|
||||
addCookie(opts: {name: string; value: string}): BrowserInterface {
|
||||
return this.chain(async () =>
|
||||
context!.addCookies([
|
||||
{
|
||||
path: "/",
|
||||
domain: await page.evaluate("window.location.hostname"),
|
||||
...opts,
|
||||
},
|
||||
]),
|
||||
)
|
||||
}
|
||||
deleteCookies(): BrowserInterface {
|
||||
return this.chain(async () => context!.clearCookies())
|
||||
}
|
||||
|
||||
focusPage() {
|
||||
return this.chain(() => page.bringToFront())
|
||||
}
|
||||
|
||||
private wrapElement(el: ElementHandle, selector: string) {
|
||||
;(el as any).selector = selector
|
||||
;(el as any).text = () => el.innerText()
|
||||
;(el as any).getComputedCss = (prop) =>
|
||||
page.evaluate(
|
||||
function (args) {
|
||||
return (
|
||||
getComputedStyle(document.querySelector(args.selector) as Element)[args.prop] || null
|
||||
)
|
||||
},
|
||||
{selector, prop},
|
||||
)
|
||||
;(el as any).getCssValue = (el as any).getComputedCss
|
||||
;(el as any).getValue = () =>
|
||||
page.evaluate(
|
||||
function (args) {
|
||||
return (document.querySelector(args.selector) as any).value
|
||||
},
|
||||
{selector},
|
||||
)
|
||||
return el
|
||||
}
|
||||
|
||||
elementByCss(selector: string) {
|
||||
return this.waitForElementByCss(selector)
|
||||
}
|
||||
|
||||
elementById(sel) {
|
||||
return this.elementByCss(`#${sel}`)
|
||||
}
|
||||
|
||||
getValue() {
|
||||
return this.chain((el) =>
|
||||
page.evaluate(
|
||||
function (args) {
|
||||
return document.querySelector(args.selector).value
|
||||
},
|
||||
{selector: el.selector},
|
||||
),
|
||||
) as any
|
||||
}
|
||||
|
||||
text() {
|
||||
return this.chain((el) => el.text()) as any
|
||||
}
|
||||
|
||||
type(text) {
|
||||
return this.chain((el) => el.type(text))
|
||||
}
|
||||
|
||||
moveTo() {
|
||||
return this.chain((el) => {
|
||||
return page.hover(el.selector).then(() => el)
|
||||
})
|
||||
}
|
||||
|
||||
async getComputedCss(prop: string) {
|
||||
return this.chain((el) => {
|
||||
return el.getCssValue(prop)
|
||||
}) as any
|
||||
}
|
||||
|
||||
async getAttribute(attr) {
|
||||
return this.chain((el) => el.getAttribute(attr))
|
||||
}
|
||||
|
||||
async hasElementByCssSelector(selector: string) {
|
||||
return this.eval(`!!document.querySelector('${selector}')`) as any
|
||||
}
|
||||
|
||||
keydown(key: string): BrowserInterface {
|
||||
return this.chain((el) => {
|
||||
return page.keyboard.down(key).then(() => el)
|
||||
})
|
||||
}
|
||||
|
||||
keyup(key: string): BrowserInterface {
|
||||
return this.chain((el) => {
|
||||
return page.keyboard.up(key).then(() => el)
|
||||
})
|
||||
}
|
||||
|
||||
click() {
|
||||
return this.chain((el) => {
|
||||
return el.click().then(() => el)
|
||||
})
|
||||
}
|
||||
|
||||
elementsByCss(sel) {
|
||||
return this.chain(() =>
|
||||
page.$$(sel).then((els) => {
|
||||
return els.map((el) => {
|
||||
const origGetAttribute = el.getAttribute.bind(el)
|
||||
el.getAttribute = (name) => {
|
||||
// ensure getAttribute defaults to empty string to
|
||||
// match selenium
|
||||
return origGetAttribute(name).then((val) => val || "")
|
||||
}
|
||||
return el
|
||||
})
|
||||
}),
|
||||
) as any as BrowserInterface[]
|
||||
}
|
||||
|
||||
waitForElementByCss(selector, timeout?: number) {
|
||||
return this.chain(() => {
|
||||
return page.waitForSelector(selector, {timeout, state: "attached"}).then(async (el) => {
|
||||
// it seems selenium waits longer and tests rely on this behavior
|
||||
// so we wait for the load event fire before returning
|
||||
await page.waitForLoadState()
|
||||
return this.wrapElement(el, selector)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
waitForCondition(condition, timeout) {
|
||||
return this.chain(() => {
|
||||
return page.waitForFunction(condition, {timeout})
|
||||
})
|
||||
}
|
||||
|
||||
async eval(snippet) {
|
||||
// TODO: should this and evalAsync be chained? Might lead
|
||||
// to bad chains
|
||||
return page
|
||||
.evaluate(snippet)
|
||||
.catch((err) => {
|
||||
console.error("eval error:", err)
|
||||
return null
|
||||
})
|
||||
.then(async (val) => {
|
||||
await page.waitForLoadState()
|
||||
return val
|
||||
})
|
||||
}
|
||||
|
||||
async evalAsync(snippet) {
|
||||
if (typeof snippet === "function") {
|
||||
snippet = snippet.toString()
|
||||
}
|
||||
|
||||
if (snippet.includes(`var callback = arguments[arguments.length - 1]`)) {
|
||||
snippet = `(function() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const origFunc = ${snippet}
|
||||
try {
|
||||
origFunc(resolve)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})()`
|
||||
}
|
||||
|
||||
return page.evaluate(snippet).catch(() => null)
|
||||
}
|
||||
|
||||
async log() {
|
||||
return this.chain(() => pageLogs) as any
|
||||
}
|
||||
|
||||
async websocketFrames() {
|
||||
return this.chain(() => websocketFrames) as any
|
||||
}
|
||||
|
||||
async url() {
|
||||
return this.chain(() => page.evaluate("window.location.href")) as any
|
||||
}
|
||||
}
|
||||
|
||||
export default Playwright
|
||||
331
integration-tests/utils/browsers/selenium.ts
Normal file
331
integration-tests/utils/browsers/selenium.ts
Normal file
@@ -0,0 +1,331 @@
|
||||
import path from "path"
|
||||
import resolveFrom from "resolve-from"
|
||||
import {execSync} from "child_process"
|
||||
import {Options as ChromeOptions} from "selenium-webdriver/chrome"
|
||||
import {Options as SafariOptions} from "selenium-webdriver/safari"
|
||||
import {Options as FireFoxOptions} from "selenium-webdriver/firefox"
|
||||
import {Builder, By, ThenableWebDriver, until} from "selenium-webdriver"
|
||||
|
||||
import {BrowserInterface} from "./base"
|
||||
|
||||
const {
|
||||
BROWSERSTACK,
|
||||
BROWSERSTACK_USERNAME,
|
||||
BROWSERSTACK_ACCESS_KEY,
|
||||
HEADLESS,
|
||||
CHROME_BIN,
|
||||
LEGACY_SAFARI,
|
||||
SKIP_LOCAL_SELENIUM_SERVER,
|
||||
} = process.env
|
||||
|
||||
if (process.env.ChromeWebDriver) {
|
||||
process.env.PATH = `${process.env.ChromeWebDriver}${path.delimiter}${process.env.PATH}`
|
||||
}
|
||||
|
||||
let seleniumServer: any
|
||||
let browserStackLocal: any
|
||||
let browser: ThenableWebDriver
|
||||
|
||||
export async function quit() {
|
||||
await Promise.all([
|
||||
browser?.quit(),
|
||||
new Promise<void>((resolve) => {
|
||||
browserStackLocal ? browserStackLocal.killAllProcesses(() => resolve()) : resolve()
|
||||
}),
|
||||
])
|
||||
seleniumServer?.kill()
|
||||
|
||||
browser = undefined
|
||||
browserStackLocal = undefined
|
||||
seleniumServer = undefined
|
||||
}
|
||||
|
||||
class Selenium extends BrowserInterface {
|
||||
private browserName: string
|
||||
|
||||
// TODO: support setting locale
|
||||
async setup(browserName: string, locale?: string) {
|
||||
if (browser) return
|
||||
this.browserName = browserName
|
||||
|
||||
let capabilities = {}
|
||||
const isSafari = browserName === "safari"
|
||||
const isFirefox = browserName === "firefox"
|
||||
const isIE = browserName === "internet explorer"
|
||||
const isBrowserStack = BROWSERSTACK
|
||||
const localSeleniumServer = SKIP_LOCAL_SELENIUM_SERVER !== "true"
|
||||
|
||||
// install conditional packages globally so the entire
|
||||
// monorepo doesn't need to rebuild when testing
|
||||
let globalNodeModules: string
|
||||
|
||||
if (isBrowserStack || localSeleniumServer) {
|
||||
globalNodeModules = execSync("npm root -g").toString().trim()
|
||||
}
|
||||
|
||||
if (isBrowserStack) {
|
||||
const {Local} = require(resolveFrom(globalNodeModules, "browserstack-local"))
|
||||
browserStackLocal = new Local()
|
||||
|
||||
const localBrowserStackOpts = {
|
||||
key: process.env.BROWSERSTACK_ACCESS_KEY,
|
||||
// Add a unique local identifier to run parallel tests
|
||||
// on BrowserStack
|
||||
localIdentifier: new Date().getTime(),
|
||||
}
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
browserStackLocal.start(localBrowserStackOpts, (err) => {
|
||||
if (err) return reject(err)
|
||||
console.log("Started BrowserStackLocal", browserStackLocal.isRunning())
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
|
||||
const safariOpts = {
|
||||
os: "OS X",
|
||||
os_version: "Mojave",
|
||||
browser: "Safari",
|
||||
}
|
||||
const safariLegacyOpts = {
|
||||
os: "OS X",
|
||||
os_version: "Sierra",
|
||||
browserName: "Safari",
|
||||
browser_version: "10.1",
|
||||
}
|
||||
const ieOpts = {
|
||||
os: "Windows",
|
||||
os_version: "10",
|
||||
browser: "IE",
|
||||
}
|
||||
const firefoxOpts = {
|
||||
os: "Windows",
|
||||
os_version: "10",
|
||||
browser: "Firefox",
|
||||
}
|
||||
const sharedOpts = {
|
||||
"browserstack.local": true,
|
||||
"browserstack.video": false,
|
||||
"browserstack.user": BROWSERSTACK_USERNAME,
|
||||
"browserstack.key": BROWSERSTACK_ACCESS_KEY,
|
||||
"browserstack.localIdentifier": localBrowserStackOpts.localIdentifier,
|
||||
}
|
||||
|
||||
capabilities = {
|
||||
...capabilities,
|
||||
...sharedOpts,
|
||||
|
||||
...(isIE ? ieOpts : {}),
|
||||
...(isSafari ? (LEGACY_SAFARI ? safariLegacyOpts : safariOpts) : {}),
|
||||
...(isFirefox ? firefoxOpts : {}),
|
||||
}
|
||||
} else if (localSeleniumServer) {
|
||||
console.log("Installing selenium server")
|
||||
const seleniumServerMod = require(resolveFrom(globalNodeModules, "selenium-standalone"))
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
seleniumServerMod.install((err) => {
|
||||
if (err) return reject(err)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
|
||||
console.log("Starting selenium server")
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
seleniumServerMod.start((err, child) => {
|
||||
if (err) return reject(err)
|
||||
seleniumServer = child
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
console.log("Started selenium server")
|
||||
}
|
||||
|
||||
let chromeOptions = new ChromeOptions()
|
||||
let firefoxOptions = new FireFoxOptions()
|
||||
let safariOptions = new SafariOptions()
|
||||
|
||||
if (HEADLESS) {
|
||||
const screenSize = {width: 1280, height: 720}
|
||||
chromeOptions = chromeOptions.headless().windowSize(screenSize) as any
|
||||
firefoxOptions = firefoxOptions.headless().windowSize(screenSize)
|
||||
}
|
||||
|
||||
if (CHROME_BIN) {
|
||||
chromeOptions = chromeOptions.setChromeBinaryPath(path.resolve(CHROME_BIN))
|
||||
}
|
||||
|
||||
let seleniumServerUrl
|
||||
|
||||
if (isBrowserStack) {
|
||||
seleniumServerUrl = "http://hub-cloud.browserstack.com/wd/hub"
|
||||
} else if (localSeleniumServer) {
|
||||
seleniumServerUrl = `http://localhost:4444/wd/hub`
|
||||
}
|
||||
|
||||
browser = new Builder()
|
||||
.usingServer(seleniumServerUrl)
|
||||
.withCapabilities(capabilities)
|
||||
.forBrowser(browserName)
|
||||
.setChromeOptions(chromeOptions)
|
||||
.setFirefoxOptions(firefoxOptions)
|
||||
.setSafariOptions(safariOptions)
|
||||
.build()
|
||||
}
|
||||
|
||||
async get(url: string): Promise<void> {
|
||||
return browser.get(url)
|
||||
}
|
||||
|
||||
async loadPage(url: string) {
|
||||
// in chrome we use a new tab for testing
|
||||
if (this.browserName === "chrome") {
|
||||
const initialHandle = await browser.getWindowHandle()
|
||||
|
||||
await browser.switchTo().newWindow("tab")
|
||||
const newHandle = await browser.getWindowHandle()
|
||||
|
||||
await browser.switchTo().window(initialHandle)
|
||||
await browser.close()
|
||||
await browser.switchTo().window(newHandle)
|
||||
|
||||
// clean-up extra windows created from links and such
|
||||
for (const handle of await browser.getAllWindowHandles()) {
|
||||
if (handle !== newHandle) {
|
||||
await browser.switchTo().window(handle)
|
||||
await browser.close()
|
||||
}
|
||||
}
|
||||
await browser.switchTo().window(newHandle)
|
||||
} else {
|
||||
await browser.get("about:blank")
|
||||
}
|
||||
return browser.get(url)
|
||||
}
|
||||
|
||||
back(): BrowserInterface {
|
||||
return this.chain(() => {
|
||||
return browser.navigate().back()
|
||||
})
|
||||
}
|
||||
forward(): BrowserInterface {
|
||||
return this.chain(() => {
|
||||
return browser.navigate().forward()
|
||||
})
|
||||
}
|
||||
refresh(): BrowserInterface {
|
||||
return this.chain(() => {
|
||||
return browser.navigate().refresh()
|
||||
})
|
||||
}
|
||||
setDimensions({width, height}: {height: number; width: number}): BrowserInterface {
|
||||
return this.chain(() => browser.manage().window().setRect({width, height, x: 0, y: 0}))
|
||||
}
|
||||
addCookie(opts: {name: string; value: string}): BrowserInterface {
|
||||
return this.chain(() => browser.manage().addCookie(opts))
|
||||
}
|
||||
deleteCookies(): BrowserInterface {
|
||||
return this.chain(() => browser.manage().deleteAllCookies())
|
||||
}
|
||||
|
||||
elementByCss(selector: string) {
|
||||
return this.chain(() => {
|
||||
return browser.findElement(By.css(selector)).then((el: any) => {
|
||||
el.selector = selector
|
||||
el.text = () => el.getText()
|
||||
el.getComputedCss = (prop) => el.getCssValue(prop)
|
||||
el.type = (text) => el.sendKeys(text)
|
||||
el.getValue = () =>
|
||||
browser.executeScript(`return document.querySelector('${selector}').value`)
|
||||
return el
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
elementById(sel) {
|
||||
return this.elementByCss(`#${sel}`)
|
||||
}
|
||||
|
||||
getValue() {
|
||||
return this.chain((el) =>
|
||||
browser.executeScript(`return document.querySelector('${el.selector}').value`),
|
||||
) as any
|
||||
}
|
||||
|
||||
text() {
|
||||
return this.chain((el) => el.getText()) as any
|
||||
}
|
||||
|
||||
type(text) {
|
||||
return this.chain((el) => el.sendKeys(text))
|
||||
}
|
||||
|
||||
moveTo() {
|
||||
return this.chain((el) => {
|
||||
return browser
|
||||
.actions()
|
||||
.move({origin: el})
|
||||
.perform()
|
||||
.then(() => el)
|
||||
})
|
||||
}
|
||||
|
||||
async getComputedCss(prop: string) {
|
||||
return this.chain((el) => {
|
||||
return el.getCssValue(prop)
|
||||
}) as any
|
||||
}
|
||||
|
||||
async getAttribute(attr) {
|
||||
return this.chain((el) => el.getAttribute(attr))
|
||||
}
|
||||
|
||||
async hasElementByCssSelector(selector: string) {
|
||||
return this.eval(`!!document.querySelector('${selector}')`) as any
|
||||
}
|
||||
|
||||
click() {
|
||||
return this.chain((el) => {
|
||||
return el.click().then(() => el)
|
||||
})
|
||||
}
|
||||
|
||||
elementsByCss(sel) {
|
||||
return this.chain(() => browser.findElements(By.css(sel))) as any as BrowserInterface[]
|
||||
}
|
||||
|
||||
waitForElementByCss(sel, timeout) {
|
||||
return this.chain(() => browser.wait(until.elementLocated(By.css(sel)), timeout))
|
||||
}
|
||||
|
||||
waitForCondition(condition, timeout) {
|
||||
return this.chain(() =>
|
||||
browser.wait(async (driver) => {
|
||||
return driver.executeScript("return " + condition).catch(() => false)
|
||||
}, timeout),
|
||||
)
|
||||
}
|
||||
|
||||
async eval(snippet) {
|
||||
if (typeof snippet === "string" && !snippet.startsWith("return")) {
|
||||
snippet = `return ${snippet}`
|
||||
}
|
||||
return browser.executeScript(snippet)
|
||||
}
|
||||
|
||||
async evalAsync(snippet) {
|
||||
if (typeof snippet === "string" && !snippet.startsWith("return")) {
|
||||
snippet = `return ${snippet}`
|
||||
}
|
||||
return browser.executeAsyncScript(snippet)
|
||||
}
|
||||
|
||||
async log() {
|
||||
return this.chain(() => browser.manage().logs().get("browser")) as any
|
||||
}
|
||||
|
||||
async url() {
|
||||
return this.chain(() => browser.getCurrentUrl()) as any
|
||||
}
|
||||
}
|
||||
|
||||
export default Selenium
|
||||
@@ -1,30 +1,33 @@
|
||||
import spawn from "cross-spawn"
|
||||
import {ChildProcess} from "child_process"
|
||||
import express from "express"
|
||||
import {existsSync, readFileSync, unlinkSync, writeFileSync, createReadStream} from "fs"
|
||||
import {existsSync, readFileSync, createReadStream} from "fs"
|
||||
import {writeFile} from "fs-extra"
|
||||
import getPort from "get-port"
|
||||
import http from "http"
|
||||
// `next` here is the symlink in `test/node_modules/next` which points to the root directory.
|
||||
// This is done so that requiring from `next` works.
|
||||
// The reason we don't import the relative path `../../dist/<etc>` is that it would lead to inconsistent module singletons
|
||||
import https from "https"
|
||||
import server from "next/dist/server/next"
|
||||
import _pkg from "next/package.json"
|
||||
import fetch from "node-fetch"
|
||||
import path from "path"
|
||||
import qs from "querystring"
|
||||
import treeKill from "tree-kill"
|
||||
|
||||
import {readJSONSync} from "fs-extra"
|
||||
// import {packageDirectorySync} from "pkg-dir"
|
||||
import pkgDir from "pkg-dir"
|
||||
import resolveCwd from "resolve-cwd"
|
||||
export const nextServer = server
|
||||
|
||||
// polyfill Object.fromEntries for the test/integration/relay-analytics tests
|
||||
// on node 10, this can be removed after we no longer support node 10
|
||||
if (!Object.fromEntries) {
|
||||
Object.fromEntries = require("core-js/features/object/from-entries")
|
||||
}
|
||||
export const pkg = _pkg
|
||||
|
||||
export function initNextServerScript(scriptPath, successRegexp, env, failRegexp, opts) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const instance = spawn("node", ["--no-deprecation", scriptPath], {env})
|
||||
const instance = spawn(
|
||||
"node",
|
||||
[...((opts && opts.nodeArgs) || []), "--no-deprecation", scriptPath],
|
||||
{
|
||||
env,
|
||||
cwd: opts && opts.cwd,
|
||||
},
|
||||
)
|
||||
|
||||
function handleStdout(data) {
|
||||
const message = data.toString()
|
||||
@@ -65,6 +68,27 @@ export function initNextServerScript(scriptPath, successRegexp, env, failRegexp,
|
||||
})
|
||||
}
|
||||
|
||||
export function getFullUrl(appPortOrUrl: string | number, url: string, hostname?: string) {
|
||||
let fullUrl =
|
||||
typeof appPortOrUrl === "string" && appPortOrUrl.startsWith("http")
|
||||
? appPortOrUrl
|
||||
: `http://${hostname ? hostname : "localhost"}:${appPortOrUrl}${url}`
|
||||
|
||||
if (typeof appPortOrUrl === "string" && url) {
|
||||
const parsedUrl = new URL(fullUrl)
|
||||
const parsedPathQuery = new URL(url, fullUrl)
|
||||
|
||||
parsedUrl.search = parsedPathQuery.search
|
||||
parsedUrl.pathname = parsedPathQuery.pathname
|
||||
|
||||
if (hostname && parsedUrl.hostname === "localhost") {
|
||||
parsedUrl.hostname = hostname
|
||||
}
|
||||
fullUrl = parsedUrl.toString()
|
||||
}
|
||||
return fullUrl
|
||||
}
|
||||
|
||||
export function renderViaAPI(app, pathname, query) {
|
||||
const url = `${pathname}${query ? `?${qs.stringify(query)}` : ""}`
|
||||
return app.renderToHTML({url}, {}, pathname, query)
|
||||
@@ -75,40 +99,63 @@ export function renderViaHTTP(appPort, pathname, query, opts) {
|
||||
}
|
||||
|
||||
export function fetchViaHTTP(appPort, pathname, query, opts) {
|
||||
const url = `http://localhost:${appPort}${pathname}${
|
||||
const url = `${pathname}${
|
||||
typeof query === "string" ? query : query ? `?${qs.stringify(query)}` : ""
|
||||
}`
|
||||
return fetch(url, opts)
|
||||
return fetch(getFullUrl(appPort, url), {
|
||||
// in node.js v17 fetch favors IPv6 but Next.js is
|
||||
// listening on IPv4 by default so force IPv4 DNS resolving
|
||||
agent: (parsedUrl) => {
|
||||
if (parsedUrl.protocol === "https:") {
|
||||
return new https.Agent({family: 4})
|
||||
}
|
||||
if (parsedUrl.protocol === "http:") {
|
||||
return new http.Agent({family: 4})
|
||||
}
|
||||
},
|
||||
...opts,
|
||||
})
|
||||
}
|
||||
|
||||
export function findPort() {
|
||||
return getPort()
|
||||
}
|
||||
|
||||
interface RunNextCommandOptions {
|
||||
cwd?: string
|
||||
env?: Record<any, any>
|
||||
spawnOptions?: any
|
||||
instance?: any
|
||||
stderr?: boolean
|
||||
stdout?: boolean
|
||||
ignoreFail?: boolean
|
||||
export function resolveBin(pkg: string, executable = pkg) {
|
||||
const packageDir = pkgDir.sync()!
|
||||
if (!packageDir) throw new Error(`Could not find package.json for '${pkg}'`)
|
||||
const bin = readJSONSync(path.join(packageDir, "package.json"))
|
||||
const binPath = typeof bin === "object" ? bin.dependencies[executable] : bin
|
||||
if (!binPath) throw new Error(`No bin '${executable}' in module '${pkg}'`)
|
||||
|
||||
const fullPath = path.join(packageDir, `node_modules`, `${pkg}`)
|
||||
|
||||
return fullPath
|
||||
}
|
||||
export function getCommandBin(
|
||||
command: string,
|
||||
rootFolder: string = process.cwd(),
|
||||
_usePatched: boolean = false,
|
||||
) {
|
||||
const bin = resolveBin(command)
|
||||
return path.resolve(rootFolder, bin)
|
||||
}
|
||||
|
||||
export function runNextCommand(argv: any[], options: RunNextCommandOptions = {}) {
|
||||
const cwd = options.cwd
|
||||
export function runNextCommand(argv, options: RunNextCommandOptions = {}) {
|
||||
const nextnextbin = getCommandBin("next", options.cwd)
|
||||
const nextBin = path.join(nextnextbin, "dist/bin/next")
|
||||
const cwd = options.cwd || process.cwd()
|
||||
// Let Next.js decide the environment
|
||||
const env = {
|
||||
...process.env,
|
||||
...options.env,
|
||||
NODE_ENV: "",
|
||||
NODE_ENV: "production" as const,
|
||||
__NEXT_TEST_MODE: "true",
|
||||
...options.env,
|
||||
}
|
||||
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`Running command "next ${argv.join(" ")}"`)
|
||||
const instance = spawn("pnpm", ["exec", "next", ...argv], {
|
||||
...options.spawnOptions,
|
||||
const instance = spawn("node", [nextBin, ...argv], {
|
||||
cwd,
|
||||
env,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
@@ -118,22 +165,43 @@ export function runNextCommand(argv: any[], options: RunNextCommandOptions = {})
|
||||
options.instance(instance)
|
||||
}
|
||||
|
||||
let mergedStdio = ""
|
||||
|
||||
let stderrOutput = ""
|
||||
instance.stderr?.on("data", function (chunk) {
|
||||
stderrOutput += chunk
|
||||
})
|
||||
if (options.stderr) {
|
||||
instance.stderr?.on("data", function (chunk) {
|
||||
mergedStdio += chunk
|
||||
stderrOutput += chunk
|
||||
|
||||
if (options.stderr === "log") {
|
||||
console.log(chunk.toString())
|
||||
}
|
||||
})
|
||||
} else {
|
||||
instance.stderr?.on("data", function (chunk) {
|
||||
mergedStdio += chunk
|
||||
})
|
||||
}
|
||||
|
||||
let stdoutOutput = ""
|
||||
if (options.stdout) {
|
||||
instance.stdout?.on("data", function (chunk) {
|
||||
mergedStdio += chunk
|
||||
stdoutOutput += chunk
|
||||
|
||||
if (options.stdout === "log") {
|
||||
console.log(chunk.toString())
|
||||
}
|
||||
})
|
||||
} else {
|
||||
instance.stdout?.on("data", function (chunk) {
|
||||
mergedStdio += chunk
|
||||
})
|
||||
}
|
||||
|
||||
instance.on("close", (code, signal) => {
|
||||
if (!options.stderr && !options.stdout && !options.ignoreFail && code !== 0) {
|
||||
console.log(stderrOutput)
|
||||
return reject(new Error(`command failed with code ${code}`))
|
||||
return reject(new Error(`command failed with code ${code}\n${mergedStdio}`))
|
||||
}
|
||||
|
||||
resolve({
|
||||
@@ -145,7 +213,6 @@ export function runNextCommand(argv: any[], options: RunNextCommandOptions = {})
|
||||
})
|
||||
|
||||
instance.on("error", (err: any) => {
|
||||
console.log(stderrOutput)
|
||||
err.stdout = stdoutOutput
|
||||
err.stderr = stderrOutput
|
||||
reject(err)
|
||||
@@ -153,41 +220,41 @@ export function runNextCommand(argv: any[], options: RunNextCommandOptions = {})
|
||||
})
|
||||
}
|
||||
|
||||
interface RunNextCommandDevOptions {
|
||||
cwd?: string
|
||||
env?: Record<any, any>
|
||||
onStdout?: (stdout: string) => void
|
||||
onStderr?: (stderr: string) => void
|
||||
stderr?: boolean
|
||||
stdout?: boolean
|
||||
nodeArgs?: []
|
||||
bootupMarker?: any
|
||||
nextStart?: boolean
|
||||
}
|
||||
export function runNextCommandDev(argv, stdOut, opts: RunNextCommandDevOptions = {}) {
|
||||
const nextnextbin = getCommandBin("next", opts.cwd)
|
||||
const nextDir = path.resolve(require.resolve("next/package"))
|
||||
const nextBin = path.join(nextnextbin, "dist/bin/next")
|
||||
|
||||
export function runNextCommandDev(argv, opts: RunNextCommandDevOptions = {}) {
|
||||
const cwd = opts.cwd // || nextDir
|
||||
const cwd = opts.cwd || nextDir
|
||||
const env = {
|
||||
...process.env,
|
||||
NODE_ENV: opts.nextStart ? ("production" as const) : ("development" as const),
|
||||
NODE_ENV: undefined,
|
||||
__NEXT_TEST_MODE: "true",
|
||||
...opts.env,
|
||||
}
|
||||
|
||||
return new Promise<void | string | ChildProcess>((resolve, reject) => {
|
||||
const instance = spawn("pnpm", ["exec", "next", ...argv], {
|
||||
const nodeArgs = opts.nodeArgs || []
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const instance = spawn("node", [...nodeArgs, "--no-deprecation", nextBin, ...argv], {
|
||||
cwd,
|
||||
env,
|
||||
})
|
||||
|
||||
} as {})
|
||||
let didResolve = false
|
||||
|
||||
function handleStdout(data) {
|
||||
const message = data.toString()
|
||||
|
||||
if (!didResolve) {
|
||||
didResolve = true
|
||||
resolve(instance)
|
||||
const bootupMarkers = {
|
||||
dev: /compiled .*successfully/i,
|
||||
start: /started server/i,
|
||||
}
|
||||
if (
|
||||
(opts.bootupMarker && opts.bootupMarker.test(message)) ||
|
||||
bootupMarkers[opts.nextStart || stdOut ? "start" : "dev"].test(message)
|
||||
) {
|
||||
if (!didResolve) {
|
||||
didResolve = true
|
||||
resolve(stdOut ? message : instance)
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof opts.onStdout === "function") {
|
||||
@@ -229,43 +296,42 @@ export function runNextCommandDev(argv, opts: RunNextCommandDevOptions = {}) {
|
||||
}
|
||||
|
||||
// Launch the app in dev mode.
|
||||
export function launchApp(dir, port, opts) {
|
||||
return runNextCommandDev(["-p", port], {cwd: dir, ...opts})
|
||||
export function launchApp(dir, port, opts: RunNextCommandDevOptions) {
|
||||
return runNextCommandDev([dir, "-p", port], undefined, opts)
|
||||
}
|
||||
|
||||
export function nextBuild(dir, args = [], opts = {}) {
|
||||
return runNextCommand(["build", ...args], {cwd: dir, ...opts})
|
||||
export function nextBuild(dir, args = [], opts = {}): any {
|
||||
return runNextCommand(["build", dir, ...args], opts)
|
||||
}
|
||||
|
||||
export function nextExport(dir, {outdir}, opts = {}) {
|
||||
return runNextCommand(["export", dir, "--outdir", outdir], {cwd: dir, ...opts})
|
||||
return runNextCommand(["export", dir, "--outdir", outdir], opts)
|
||||
}
|
||||
|
||||
export function nextExportDefault(dir, opts = {}) {
|
||||
return runNextCommand(["export", dir], {cwd: dir, ...opts})
|
||||
return runNextCommand(["export", dir], opts)
|
||||
}
|
||||
|
||||
export function nextLint(dir, args = [], opts = {}) {
|
||||
return runNextCommand(["lint", dir, ...args], {cwd: dir, ...opts})
|
||||
return runNextCommand(["lint", dir, ...args], opts)
|
||||
}
|
||||
|
||||
export function nextStart(dir, port, opts = {}) {
|
||||
return runNextCommandDev(["start", "-p", port], {
|
||||
cwd: dir,
|
||||
return runNextCommandDev(["start", "-p", port, dir], undefined, {
|
||||
...opts,
|
||||
nextStart: true,
|
||||
})
|
||||
}
|
||||
|
||||
export function buildTS(args = [], cwd, env = {NODE_ENV: "production" as const}) {
|
||||
cwd = cwd
|
||||
env = {...process.env, ...env}
|
||||
export function buildTS(args = [], cwd, env = {}) {
|
||||
cwd = cwd || path.dirname(require.resolve("next/package"))
|
||||
env = {...process.env, NODE_ENV: undefined, ...env}
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const instance = spawn(
|
||||
"node",
|
||||
["--no-deprecation", require.resolve("typescript/lib/tsc"), ...args],
|
||||
{cwd, env},
|
||||
{cwd, env} as {},
|
||||
)
|
||||
let output = ""
|
||||
|
||||
@@ -285,10 +351,9 @@ export function buildTS(args = [], cwd, env = {NODE_ENV: "production" as const})
|
||||
})
|
||||
}
|
||||
|
||||
// Kill a launched app
|
||||
export async function killApp(instance) {
|
||||
export async function killProcess(pid) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
treeKill(instance.pid, (err) => {
|
||||
treeKill(pid, (err) => {
|
||||
if (err) {
|
||||
if (
|
||||
process.platform === "win32" &&
|
||||
@@ -311,11 +376,25 @@ export async function killApp(instance) {
|
||||
})
|
||||
}
|
||||
|
||||
export async function startApp(app: any) {
|
||||
// Kill a launched app
|
||||
export async function killApp(instance) {
|
||||
if (instance) {
|
||||
await killProcess(instance.pid)
|
||||
}
|
||||
}
|
||||
|
||||
export async function startApp(app) {
|
||||
// force require usage instead of dynamic import in jest
|
||||
// x-ref: https://github.com/nodejs/node/issues/35889
|
||||
process.env.__NEXT_TEST_MODE = "jest"
|
||||
|
||||
// TODO: tests that use this should be migrated to use
|
||||
// the nextStart test function instead as it tests outside
|
||||
// of jest's context
|
||||
await app.prepare()
|
||||
const handler = app.getRequestHandler()
|
||||
const server = http.createServer(handler)
|
||||
;(server as any).__app = app
|
||||
const server: any = http.createServer(handler)
|
||||
server.__app = app
|
||||
|
||||
await promiseCall(server, "listen")
|
||||
return server
|
||||
@@ -346,7 +425,7 @@ export function waitFor(millis) {
|
||||
return new Promise((resolve) => setTimeout(resolve, millis))
|
||||
}
|
||||
|
||||
export async function startStaticServer(dir, notFoundFile) {
|
||||
export async function startStaticServer(dir, notFoundFile, fixedPort) {
|
||||
const app = express()
|
||||
const server = http.createServer(app)
|
||||
app.use(express.static(dir))
|
||||
@@ -357,7 +436,7 @@ export async function startStaticServer(dir, notFoundFile) {
|
||||
})
|
||||
}
|
||||
|
||||
await promiseCall(server, "listen")
|
||||
await promiseCall(server, "listen", fixedPort)
|
||||
return server
|
||||
}
|
||||
|
||||
@@ -401,55 +480,9 @@ export async function check(contentFn, regex, hardError = true) {
|
||||
return false
|
||||
}
|
||||
|
||||
export class File {
|
||||
path: string
|
||||
originalContent: any
|
||||
constructor(path) {
|
||||
this.path = path
|
||||
this.originalContent = existsSync(this.path) ? readFileSync(this.path, "utf8") : null
|
||||
}
|
||||
|
||||
write(content) {
|
||||
if (!this.originalContent) {
|
||||
this.originalContent = content
|
||||
}
|
||||
writeFileSync(this.path, content, "utf8")
|
||||
}
|
||||
|
||||
replace(pattern, newValue) {
|
||||
const currentContent = readFileSync(this.path, "utf8")
|
||||
if (pattern instanceof RegExp) {
|
||||
if (!pattern.test(currentContent)) {
|
||||
throw new Error(
|
||||
`Failed to replace content.\n\nPattern: ${pattern.toString()}\n\nContent: ${currentContent}`,
|
||||
)
|
||||
}
|
||||
} else if (typeof pattern === "string") {
|
||||
if (!currentContent.includes(pattern)) {
|
||||
throw new Error(
|
||||
`Failed to replace content.\n\nPattern: ${pattern}\n\nContent: ${currentContent}`,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
throw new Error(`Unknown replacement attempt type: ${pattern}`)
|
||||
}
|
||||
|
||||
const newContent = currentContent.replace(pattern, newValue)
|
||||
this.write(newContent)
|
||||
}
|
||||
|
||||
delete() {
|
||||
unlinkSync(this.path)
|
||||
}
|
||||
|
||||
restore() {
|
||||
this.write(this.originalContent)
|
||||
}
|
||||
}
|
||||
|
||||
export async function evaluate(browser, input) {
|
||||
if (typeof input === "function") {
|
||||
const result = await browser.executeScript(input)
|
||||
const result = await browser.eval(input)
|
||||
await new Promise((resolve) => setTimeout(resolve, 30))
|
||||
return result
|
||||
} else {
|
||||
@@ -479,9 +512,8 @@ export async function retry(fn, duration = 3000, interval = 500, description) {
|
||||
}
|
||||
|
||||
export async function hasRedbox(browser, expected = true) {
|
||||
let attempts = 30
|
||||
do {
|
||||
const has = await evaluate(browser, () => {
|
||||
for (let i = 0; i < 30; i++) {
|
||||
const result = await evaluate(browser, () => {
|
||||
return Boolean(
|
||||
[].slice
|
||||
.call(document.querySelectorAll("nextjs-portal"))
|
||||
@@ -492,15 +524,12 @@ export async function hasRedbox(browser, expected = true) {
|
||||
),
|
||||
)
|
||||
})
|
||||
if (has) {
|
||||
return true
|
||||
}
|
||||
if (--attempts < 0) {
|
||||
break
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000))
|
||||
} while (expected)
|
||||
if (result === expected) {
|
||||
return result
|
||||
}
|
||||
await waitFor(1000)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -544,6 +573,24 @@ export async function getRedboxSource(browser) {
|
||||
)
|
||||
}
|
||||
|
||||
export async function getRedboxDescription(browser) {
|
||||
return retry(
|
||||
() =>
|
||||
evaluate(browser, () => {
|
||||
const portal = [].slice
|
||||
.call(document.querySelectorAll("nextjs-portal"))
|
||||
.find((p) => p.shadowRoot.querySelector("[data-nextjs-dialog-header]"))
|
||||
const root = portal.shadowRoot
|
||||
return root
|
||||
.querySelector("#nextjs__container_errors_desc")
|
||||
.innerText.replace(/__WEBPACK_DEFAULT_EXPORT__/, "Unknown")
|
||||
}),
|
||||
3000,
|
||||
500,
|
||||
"getRedboxDescription",
|
||||
)
|
||||
}
|
||||
|
||||
export function getBrowserBodyText(browser) {
|
||||
return browser.eval('document.getElementsByTagName("body")[0].innerText')
|
||||
}
|
||||
@@ -552,8 +599,8 @@ export function normalizeRegEx(src) {
|
||||
return new RegExp(src).source.replace(/\^\//g, "^\\/")
|
||||
}
|
||||
|
||||
function readJson(path: string) {
|
||||
return JSON.parse(readFileSync(path) as any)
|
||||
function readJson(path) {
|
||||
return JSON.parse(readFileSync(path).toString())
|
||||
}
|
||||
|
||||
export function getBuildManifest(dir) {
|
||||
@@ -614,3 +661,96 @@ export function readNextBuildServerPageFile(appDir, page) {
|
||||
const pageFile = getPageFileFromPagesManifest(appDir, page)
|
||||
return readFileSync(path.join(appDir, ".next", "server", pageFile), "utf8")
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} suiteName
|
||||
* @param {{env: 'prod' | 'dev', appDir: string}} context
|
||||
* @param {{beforeAll?: Function; afterAll?: Function; runTests: Function}} options
|
||||
*/
|
||||
function runSuite(suiteName, context, options) {
|
||||
const {appDir, env} = context
|
||||
describe(`${suiteName} ${env}`, () => {
|
||||
beforeAll(async () => {
|
||||
options.beforeAll?.(env)
|
||||
context.stderr = ""
|
||||
const onStderr = (msg) => {
|
||||
context.stderr += msg
|
||||
}
|
||||
context.stdout = ""
|
||||
const onStdout = (msg) => {
|
||||
context.stdout += msg
|
||||
}
|
||||
if (env === "prod") {
|
||||
context.appPort = await findPort()
|
||||
const {stdout, stderr, code} = await nextBuild(appDir, [], {
|
||||
stderr: true,
|
||||
stdout: true,
|
||||
})
|
||||
context.stdout = stdout
|
||||
context.stderr = stderr
|
||||
context.code = code
|
||||
context.server = await nextStart(context.appDir, context.appPort, {
|
||||
onStderr,
|
||||
onStdout,
|
||||
})
|
||||
} else if (env === "dev") {
|
||||
context.appPort = await findPort()
|
||||
context.server = await launchApp(context.appDir, context.appPort, {
|
||||
onStderr,
|
||||
onStdout,
|
||||
})
|
||||
}
|
||||
})
|
||||
afterAll(async () => {
|
||||
options.afterAll?.(env)
|
||||
if (context.server) {
|
||||
await killApp(context.server)
|
||||
}
|
||||
})
|
||||
options.runTests(context, env)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} suiteName
|
||||
* @param {string} appDir
|
||||
* @param {{beforeAll?: Function; afterAll?: Function; runTests: Function}} options
|
||||
*/
|
||||
export function runDevSuite(suiteName, appDir, options) {
|
||||
return runSuite(suiteName, {appDir, env: "dev"}, options)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} suiteName
|
||||
* @param {string} appDir
|
||||
* @param {{beforeAll?: Function; afterAll?: Function; runTests: Function}} options
|
||||
*/
|
||||
export function runProdSuite(suiteName, appDir, options) {
|
||||
return runSuite(suiteName, {appDir, env: "prod"}, options)
|
||||
}
|
||||
|
||||
interface RunNextCommandOptions {
|
||||
cwd?: string
|
||||
env?: Record<any, any>
|
||||
spawnOptions?: any
|
||||
instance?: any
|
||||
stderr?: string
|
||||
stdout?: string
|
||||
ignoreFail?: boolean
|
||||
nodeArgs?: []
|
||||
}
|
||||
|
||||
interface RunNextCommandDevOptions {
|
||||
cwd?: string
|
||||
env?: Record<any, any>
|
||||
onStdout?: (stdout: string) => void
|
||||
onStderr?: (stderr: string) => void
|
||||
stderr?: string | boolean
|
||||
stdout?: string | boolean
|
||||
nodeArgs?: []
|
||||
bootupMarker?: any
|
||||
nextStart?: boolean
|
||||
}
|
||||
|
||||
@@ -1,236 +1,105 @@
|
||||
/// <reference types="./next-webdriver" />
|
||||
import fetch from "node-fetch"
|
||||
import os from "os"
|
||||
import path from "path"
|
||||
import {Builder, By} from "selenium-webdriver"
|
||||
import {Options as ChromeOptions} from "selenium-webdriver/chrome"
|
||||
import {Options as FireFoxOptions} from "selenium-webdriver/firefox"
|
||||
import {Options as SafariOptions} from "selenium-webdriver/safari"
|
||||
import Chain from "./wd-chain"
|
||||
import {getFullUrl} from "./next-test-utils"
|
||||
import {BrowserInterface} from "./browsers/base"
|
||||
import browserMod, {quit} from "./browsers/playwright"
|
||||
;(global as any).browserName = process.env.BROWSER_NAME || "chrome"
|
||||
|
||||
export function waitFor(millis) {
|
||||
return new Promise((resolve) => setTimeout(resolve, millis))
|
||||
}
|
||||
let browserQuit
|
||||
|
||||
export {By}
|
||||
|
||||
const {
|
||||
BROWSER_NAME: browserName = "chrome",
|
||||
BROWSERSTACK,
|
||||
BROWSERSTACK_USERNAME,
|
||||
BROWSERSTACK_ACCESS_KEY,
|
||||
HEADLESS,
|
||||
CHROME_BIN,
|
||||
LEGACY_SAFARI,
|
||||
CHROMEWEBDRIVER,
|
||||
} = process.env
|
||||
|
||||
let capabilities = {}
|
||||
|
||||
const isChrome = browserName === "chrome"
|
||||
const isSafari = browserName === "safari"
|
||||
const isFirefox = browserName === "firefox"
|
||||
const isIE = browserName === "internet explorer"
|
||||
|
||||
if (CHROMEWEBDRIVER) {
|
||||
console.log("path", process.env.PATH)
|
||||
console.log("comspec", process.env.COMSPEC)
|
||||
console.log("chromedriver", `${CHROMEWEBDRIVER}${path.delimiter}${process.env.PATH}`)
|
||||
}
|
||||
|
||||
if (process.env.ChromeWebDriver) {
|
||||
process.env.PATH = `${process.env.ChromeWebDriver}${path.delimiter}${process.env.PATH}`
|
||||
}
|
||||
|
||||
const isBrowserStack = BROWSERSTACK && BROWSERSTACK_USERNAME && BROWSERSTACK_ACCESS_KEY
|
||||
|
||||
if (isBrowserStack) {
|
||||
const safariOpts = {
|
||||
os: "OS X",
|
||||
os_version: "Mojave",
|
||||
browser: "Safari",
|
||||
}
|
||||
const safariLegacyOpts = {
|
||||
os: "OS X",
|
||||
os_version: "Sierra",
|
||||
browserName: "Safari",
|
||||
browser_version: "10.1",
|
||||
}
|
||||
const ieOpts = {
|
||||
os: "Windows",
|
||||
os_version: "10",
|
||||
browser: "IE",
|
||||
}
|
||||
const firefoxOpts = {
|
||||
os: "Windows",
|
||||
os_version: "10",
|
||||
browser: "Firefox",
|
||||
}
|
||||
const sharedOpts = {
|
||||
"browserstack.local": true,
|
||||
"browserstack.video": false,
|
||||
"browserstack.user": BROWSERSTACK_USERNAME,
|
||||
"browserstack.key": BROWSERSTACK_ACCESS_KEY,
|
||||
"browserstack.localIdentifier": global.browserStackLocalId,
|
||||
}
|
||||
|
||||
capabilities = {
|
||||
...capabilities,
|
||||
...sharedOpts,
|
||||
|
||||
...(isIE ? ieOpts : {}),
|
||||
...(isSafari ? (LEGACY_SAFARI ? safariLegacyOpts : safariOpts) : {}),
|
||||
...(isFirefox ? firefoxOpts : {}),
|
||||
}
|
||||
}
|
||||
|
||||
let chromeOptions = new ChromeOptions()
|
||||
let firefoxOptions = new FireFoxOptions()
|
||||
let safariOptions = new SafariOptions()
|
||||
|
||||
chromeOptions.addArguments("--remote-debugging-port=9222")
|
||||
chromeOptions.addArguments("--headless")
|
||||
chromeOptions.addArguments("--no-sandbox")
|
||||
chromeOptions.addArguments("--disable-gpu")
|
||||
chromeOptions.addArguments("--disable-dev-shm-usage")
|
||||
|
||||
if (HEADLESS) {
|
||||
const screenSize = {width: 1280, height: 720}
|
||||
chromeOptions = chromeOptions.headless().windowSize(screenSize)
|
||||
firefoxOptions = firefoxOptions.headless().windowSize(screenSize)
|
||||
}
|
||||
|
||||
if (CHROME_BIN) {
|
||||
console.log("chrome bin", CHROME_BIN)
|
||||
chromeOptions.setChromeBinaryPath(path.resolve(CHROME_BIN))
|
||||
}
|
||||
|
||||
let seleniumServer
|
||||
|
||||
if (isBrowserStack) {
|
||||
seleniumServer = "http://hub-cloud.browserstack.com/wd/hub"
|
||||
} else if (global.seleniumServerPort) {
|
||||
seleniumServer = `http://localhost:${global.seleniumServerPort}/wd/hub`
|
||||
}
|
||||
|
||||
let browser = new Builder()
|
||||
.usingServer(seleniumServer)
|
||||
.withCapabilities(capabilities)
|
||||
.forBrowser(browserName)
|
||||
.setChromeOptions(chromeOptions)
|
||||
.setFirefoxOptions(firefoxOptions)
|
||||
.setSafariOptions(safariOptions)
|
||||
.build()
|
||||
|
||||
global.wd = browser
|
||||
|
||||
let initialWindow
|
||||
let deviceIP = "localhost"
|
||||
|
||||
const getDeviceIP = async () => {
|
||||
const networkIntfs = os.networkInterfaces()
|
||||
// find deviceIP to use with BrowserStack
|
||||
for (const intf of Object.keys(networkIntfs)) {
|
||||
const addresses = networkIntfs[intf]
|
||||
|
||||
for (const {internal, address, family} of addresses || []) {
|
||||
if (family !== "IPv4" || internal) continue
|
||||
try {
|
||||
const res = await fetch(`http://${address}:${global._newTabPort}`)
|
||||
if (res.ok) {
|
||||
deviceIP = address
|
||||
break
|
||||
}
|
||||
} catch (_) {}
|
||||
if (typeof afterAll === "function") {
|
||||
afterAll(async () => {
|
||||
if (browserQuit) {
|
||||
await browserQuit()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const closeBrowser = async () => {
|
||||
// First we close all extra windows left over
|
||||
let allWindows = await browser.getAllWindowHandles()
|
||||
|
||||
for (const win of allWindows) {
|
||||
if (win === initialWindow) continue
|
||||
try {
|
||||
await browser.switchTo().window(win)
|
||||
await browser.close()
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const freshWindow = async (appPort) => {
|
||||
// First we close all extra windows left over
|
||||
let allWindows = await browser.getAllWindowHandles()
|
||||
|
||||
for (const win of allWindows) {
|
||||
if (win === initialWindow) continue
|
||||
try {
|
||||
await browser.switchTo().window(win)
|
||||
await browser.close()
|
||||
} catch (_) {}
|
||||
}
|
||||
await browser.switchTo().window(initialWindow)
|
||||
|
||||
// now we open a fresh window
|
||||
await browser.get(`http://${deviceIP}:${global._newTabPort}`)
|
||||
|
||||
// await browser.executeScript(`window.location.href = "http://${deviceIP}:${appPort}"`)
|
||||
|
||||
const newTabLink = await browser.findElement(By.css("#new"))
|
||||
await newTabLink.click()
|
||||
|
||||
allWindows = await browser.getAllWindowHandles()
|
||||
const newWindow = allWindows.find((win) => win !== initialWindow)
|
||||
await browser.switchTo().window(newWindow!)
|
||||
}
|
||||
export const USE_SELENIUM = Boolean(
|
||||
process.env.LEGACY_SAFARI ||
|
||||
process.env.BROWSER_NAME === "internet explorer" ||
|
||||
process.env.SKIP_LOCAL_SELENIUM_SERVER,
|
||||
)
|
||||
|
||||
/**
|
||||
*
|
||||
* @param appPortOrUrl can either be the port or the full URL
|
||||
* @param url the path/query to append when using appPort
|
||||
* @param options.waitHydration whether to wait for react hydration to finish
|
||||
* @param options.retryWaitHydration allow retrying hydration wait if reload occurs
|
||||
* @param options.disableCache disable cache for page load
|
||||
* @param options.beforePageLoad the callback receiving page instance before loading page
|
||||
* @returns thenable browser instance
|
||||
*/
|
||||
export default async function webdriver(
|
||||
appPort,
|
||||
path,
|
||||
waitHydration = true,
|
||||
allowHydrationRetry = false,
|
||||
) {
|
||||
if (!initialWindow) {
|
||||
initialWindow = await browser.getWindowHandle()
|
||||
appPortOrUrl: string | number,
|
||||
url: string,
|
||||
options?: {
|
||||
waitHydration?: boolean
|
||||
retryWaitHydration?: boolean
|
||||
disableCache?: boolean
|
||||
beforePageLoad?: (page: any) => void
|
||||
locale?: string
|
||||
},
|
||||
): Promise<BrowserInterface> {
|
||||
let CurrentInterface: typeof BrowserInterface
|
||||
|
||||
const defaultOptions = {
|
||||
waitHydration: true,
|
||||
retryWaitHydration: false,
|
||||
disableCache: false,
|
||||
}
|
||||
if (isBrowserStack && deviceIP === "localhost" && !LEGACY_SAFARI) {
|
||||
await getDeviceIP()
|
||||
}
|
||||
// browser.switchTo().window() fails with `missing field `handle``
|
||||
// in safari and firefox so disabling freshWindow since our
|
||||
// tests shouldn't rely on it
|
||||
// if (isChrome) {
|
||||
// await freshWindow(appPort)
|
||||
options = Object.assign(defaultOptions, options)
|
||||
const {waitHydration, retryWaitHydration, disableCache, beforePageLoad, locale} = options
|
||||
|
||||
// we import only the needed interface
|
||||
// if (USE_SELENIUM) {
|
||||
// const browserMod = require('browsers/selenium')
|
||||
// CurrentInterface = browserMod.default
|
||||
// browserQuit = browserMod.quit
|
||||
// } else {
|
||||
// const browserMod = require('./browsers/playwright')
|
||||
// CurrentInterface = browserMod.default
|
||||
// browserQuit = browserMod.quit
|
||||
// }
|
||||
|
||||
const url = `http://${deviceIP}:${appPort}${path}`
|
||||
;(browser as any).initUrl = url
|
||||
console.log(`> Loading browser with ${url}`)
|
||||
CurrentInterface = browserMod
|
||||
browserQuit = quit
|
||||
|
||||
await browser.get(url)
|
||||
console.log(`> Loaded browser with ${url}`)
|
||||
const browser = new CurrentInterface()
|
||||
const browserName = process.env.BROWSER_NAME || "chrome"
|
||||
await browser.setup(browserName, locale)
|
||||
;(global as any).browserName = browserName
|
||||
|
||||
const fullUrl = getFullUrl(appPortOrUrl, url, "localhost")
|
||||
|
||||
console.log(`\n> Loading browser with ${fullUrl}\n`)
|
||||
|
||||
await browser.loadPage(fullUrl, {disableCache, beforePageLoad})
|
||||
console.log(`\n> Loaded browser with ${fullUrl}\n`)
|
||||
|
||||
// Wait for application to hydrate
|
||||
if (waitHydration) {
|
||||
// console.log(`\n> Waiting hydration for ${url}\n`)
|
||||
console.log(`\n> Waiting hydration for ${fullUrl}\n`)
|
||||
|
||||
const checkHydrated = async () => {
|
||||
await browser.executeAsyncScript(function () {
|
||||
await browser.evalAsync(function () {
|
||||
var callback = arguments[arguments.length - 1]
|
||||
|
||||
// if it's not a Next.js app return
|
||||
if (document.documentElement.innerHTML.indexOf("__NEXT_DATA__") === -1) {
|
||||
console.log("Not a next.js page, resolving hydrate check")
|
||||
callback()
|
||||
}
|
||||
|
||||
// TODO: should we also ensure router.isReady is true
|
||||
// by default before resolving?
|
||||
if ((window as any).__NEXT_HYDRATED) {
|
||||
console.log("Next.js page already hydrated")
|
||||
callback()
|
||||
} else {
|
||||
var timeout = setTimeout(callback, 10 * 1000)
|
||||
;(window as any).__NEXT_HYDRATED_CB = function () {
|
||||
clearTimeout(timeout)
|
||||
console.log("Next.js hydrate callback fired")
|
||||
callback()
|
||||
}
|
||||
}
|
||||
@@ -240,9 +109,9 @@ export default async function webdriver(
|
||||
try {
|
||||
await checkHydrated()
|
||||
} catch (err) {
|
||||
if (allowHydrationRetry) {
|
||||
if (retryWaitHydration) {
|
||||
// re-try in case the page reloaded during check
|
||||
await waitFor(2000)
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000))
|
||||
await checkHydrated()
|
||||
} else {
|
||||
console.error("failed to check hydration")
|
||||
@@ -250,17 +119,7 @@ export default async function webdriver(
|
||||
}
|
||||
}
|
||||
|
||||
// console.log(`\n> Hydration complete for ${url}\n`)
|
||||
console.log(`\n> Hydration complete for ${fullUrl}\n`)
|
||||
}
|
||||
|
||||
const promiseProp = new Set(["then", "catch", "finally"])
|
||||
|
||||
return new Proxy(new Chain(browser), {
|
||||
get(obj, prop) {
|
||||
if (obj[prop] || promiseProp.has(prop as string)) {
|
||||
return obj[prop]
|
||||
}
|
||||
return browser[prop]
|
||||
},
|
||||
})
|
||||
return browser
|
||||
}
|
||||
|
||||
@@ -19,9 +19,13 @@
|
||||
"fs-extra": "10.0.1",
|
||||
"get-port": "6.1.2",
|
||||
"node-fetch": "3.2.3",
|
||||
"pkg-dir": "5.0.0",
|
||||
"playwright-chromium": "1.14.1",
|
||||
"react": "18.0.0",
|
||||
"react-dom": "18.0.0",
|
||||
"react-query": "3.39.0",
|
||||
"resolve-cwd": "3.0.0",
|
||||
"resolve-from": "5.0.0",
|
||||
"rimraf": "3.0.2",
|
||||
"selenium-webdriver": "4.1.1",
|
||||
"tree-kill": "1.2.2",
|
||||
|
||||
@@ -1,5 +1,120 @@
|
||||
# @blitzjs/auth
|
||||
|
||||
## 2.0.0-alpha.44
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 7817fe3a: Add missing RouteUrlObject on Page.authenticate.redirectTo
|
||||
- blitz@2.0.0-alpha.44
|
||||
|
||||
## 2.0.0-alpha.43
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [527e48ac]
|
||||
- blitz@2.0.0-alpha.43
|
||||
|
||||
## 2.0.0-alpha.42
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.42
|
||||
|
||||
## 2.0.0-alpha.41
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.41
|
||||
|
||||
## 2.0.0-alpha.40
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.40
|
||||
|
||||
## 2.0.0-alpha.39
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [b918055b]
|
||||
- blitz@2.0.0-alpha.39
|
||||
|
||||
## 2.0.0-alpha.38
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.38
|
||||
|
||||
## 2.0.0-alpha.37
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- a80d2a8f: rename middleware type for blitz server plugin
|
||||
- Updated dependencies [a80d2a8f]
|
||||
- blitz@2.0.0-alpha.37
|
||||
|
||||
## 2.0.0-alpha.36
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.36
|
||||
|
||||
## 2.0.0-alpha.35
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.35
|
||||
|
||||
## 2.0.0-alpha.34
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.34
|
||||
|
||||
## 2.0.0-alpha.33
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.33
|
||||
|
||||
## 2.0.0-alpha.32
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.32
|
||||
|
||||
## 2.0.0-alpha.31
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.31
|
||||
|
||||
## 2.0.0-alpha.30
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [ce453683]
|
||||
- blitz@2.0.0-alpha.30
|
||||
|
||||
## 2.0.0-alpha.29
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [962eb58a]
|
||||
- blitz@2.0.0-alpha.29
|
||||
|
||||
## 2.0.0-alpha.28
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.28
|
||||
|
||||
## 2.0.0-alpha.27
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.27
|
||||
|
||||
## 2.0.0-alpha.26
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/auth",
|
||||
"version": "2.0.0-alpha.26",
|
||||
"version": "2.0.0-alpha.44",
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts",
|
||||
@@ -26,7 +26,7 @@
|
||||
"@types/secure-password": "3.1.1",
|
||||
"b64-lite": "1.4.0",
|
||||
"bad-behavior": "1.0.1",
|
||||
"blitz": "2.0.0-alpha.26",
|
||||
"blitz": "2.0.0-alpha.44",
|
||||
"cookie": "0.4.1",
|
||||
"cookie-session": "2.0.0",
|
||||
"debug": "4.3.3",
|
||||
@@ -39,7 +39,7 @@
|
||||
"url": "0.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.26",
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.44",
|
||||
"@testing-library/react": "13.0.0",
|
||||
"@testing-library/react-hooks": "7.0.2",
|
||||
"@types/cookie": "0.4.1",
|
||||
|
||||
@@ -12,6 +12,8 @@ import {
|
||||
createClientPlugin,
|
||||
AuthenticationError,
|
||||
RedirectError,
|
||||
RouteUrlObject,
|
||||
Ctx,
|
||||
} from "blitz"
|
||||
import {
|
||||
COOKIE_CSRF_TOKEN,
|
||||
@@ -26,7 +28,6 @@ import {
|
||||
} from "../shared"
|
||||
import _debug from "debug"
|
||||
import {formatWithValidation} from "../shared/url-utils"
|
||||
import {Ctx} from "blitz"
|
||||
import {ComponentType} from "react"
|
||||
import {ComponentProps} from "react"
|
||||
|
||||
@@ -190,9 +191,9 @@ export const useRedirectAuthenticated = (to: UrlObject | string) => {
|
||||
}
|
||||
}
|
||||
|
||||
export interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
|
||||
pathname: string
|
||||
}
|
||||
// export interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
|
||||
// pathname: string
|
||||
// }
|
||||
|
||||
export type RedirectAuthenticatedTo = string | RouteUrlObject | false
|
||||
export type RedirectAuthenticatedToFnCtx = {
|
||||
@@ -204,7 +205,7 @@ export type RedirectAuthenticatedToFn = (
|
||||
|
||||
export type BlitzPage<P = {}> = React.ComponentType<P> & {
|
||||
getLayout?: (component: JSX.Element) => JSX.Element
|
||||
authenticate?: boolean | {redirectTo?: string}
|
||||
authenticate?: boolean | {redirectTo?: string | RouteUrlObject}
|
||||
suppressFirstRenderFlicker?: boolean
|
||||
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type {BlitzServerPlugin, Middleware, Ctx} from "blitz"
|
||||
import type {BlitzServerPlugin, RequestMiddleware, Ctx} from "blitz"
|
||||
import {assert} from "blitz"
|
||||
import {IncomingMessage, ServerResponse} from "http"
|
||||
import {PublicData, SessionModel, SessionConfigMethods} from "../shared/types"
|
||||
@@ -101,7 +101,7 @@ export function AuthServerPlugin(options: AuthPluginOptions): BlitzServerPlugin<
|
||||
`The cookie prefix used has invalid characters. Only alphanumeric characters, "-" and "_" character are supported`,
|
||||
)
|
||||
|
||||
const blitzSessionMiddleware: Middleware<
|
||||
const blitzSessionMiddleware: RequestMiddleware<
|
||||
IncomingMessage,
|
||||
ServerResponse & {blitzCtx: Ctx}
|
||||
> = async (req, res, next) => {
|
||||
@@ -119,6 +119,6 @@ export function AuthServerPlugin(options: AuthPluginOptions): BlitzServerPlugin<
|
||||
return blitzSessionMiddleware
|
||||
}
|
||||
return {
|
||||
middlewares: [authPluginSessionMiddleware()],
|
||||
requestMiddlewares: [authPluginSessionMiddleware()],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
connectMiddleware,
|
||||
Ctx,
|
||||
handleRequestWithMiddleware,
|
||||
Middleware,
|
||||
RequestMiddleware,
|
||||
MiddlewareResponse,
|
||||
secureProxyMiddleware,
|
||||
} from "blitz"
|
||||
@@ -79,9 +79,9 @@ export function passportAuth(config: BlitzPassportConfig): ApiHandler {
|
||||
|
||||
const passportMiddleware = passport.initialize()
|
||||
|
||||
const middleware: Middleware<ApiHandlerIncomingMessage, MiddlewareResponse<Ctx>>[] = [
|
||||
connectMiddleware(cookieSessionMiddleware as Middleware),
|
||||
connectMiddleware(passportMiddleware as Middleware),
|
||||
const middleware: RequestMiddleware<ApiHandlerIncomingMessage, MiddlewareResponse<Ctx>>[] = [
|
||||
connectMiddleware(cookieSessionMiddleware as RequestMiddleware),
|
||||
connectMiddleware(passportMiddleware as RequestMiddleware),
|
||||
connectMiddleware(passport.session()),
|
||||
]
|
||||
|
||||
|
||||
@@ -1,5 +1,123 @@
|
||||
# @blitzjs/next
|
||||
|
||||
## 2.0.0-alpha.44
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 7817fe3a: Add missing RouteUrlObject on Page.authenticate.redirectTo
|
||||
- ae0b714f: Allow passing optional type argument for ParamsType in GSSP
|
||||
- @blitzjs/rpc@2.0.0-alpha.44
|
||||
|
||||
## 2.0.0-alpha.43
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.43
|
||||
|
||||
## 2.0.0-alpha.42
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 9c2e7d37: Use `useRouter` from next/router in useParams function
|
||||
- @blitzjs/rpc@2.0.0-alpha.42
|
||||
|
||||
## 2.0.0-alpha.41
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.41
|
||||
|
||||
## 2.0.0-alpha.40
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 9ded8dac: useParam & useParams functions now accessible from @blitzjs/next
|
||||
- @blitzjs/rpc@2.0.0-alpha.40
|
||||
|
||||
## 2.0.0-alpha.39
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.39
|
||||
|
||||
## 2.0.0-alpha.38
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [8aee25c5]
|
||||
- @blitzjs/rpc@2.0.0-alpha.38
|
||||
|
||||
## 2.0.0-alpha.37
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- a80d2a8f: rename middleware type for blitz server plugin
|
||||
- @blitzjs/rpc@2.0.0-alpha.37
|
||||
|
||||
## 2.0.0-alpha.36
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [4cad9cca]
|
||||
- @blitzjs/rpc@2.0.0-alpha.36
|
||||
|
||||
## 2.0.0-alpha.35
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.35
|
||||
|
||||
## 2.0.0-alpha.34
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [dfd2408e]
|
||||
- @blitzjs/rpc@2.0.0-alpha.34
|
||||
|
||||
## 2.0.0-alpha.33
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.33
|
||||
|
||||
## 2.0.0-alpha.32
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.32
|
||||
|
||||
## 2.0.0-alpha.31
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [17ce29e5]
|
||||
- @blitzjs/rpc@2.0.0-alpha.31
|
||||
|
||||
## 2.0.0-alpha.30
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.30
|
||||
|
||||
## 2.0.0-alpha.29
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.29
|
||||
|
||||
## 2.0.0-alpha.28
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/rpc@2.0.0-alpha.28
|
||||
|
||||
## 2.0.0-alpha.27
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [07292910]
|
||||
- @blitzjs/rpc@2.0.0-alpha.27
|
||||
|
||||
## 2.0.0-alpha.26
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/next",
|
||||
"version": "2.0.0-alpha.26",
|
||||
"version": "2.0.0-alpha.44",
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
|
||||
@@ -23,7 +23,7 @@
|
||||
"eslint.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"@blitzjs/rpc": "2.0.0-alpha.26",
|
||||
"@blitzjs/rpc": "2.0.0-alpha.44",
|
||||
"@types/hoist-non-react-statics": "3.3.1",
|
||||
"debug": "4.3.3",
|
||||
"fs-extra": "10.0.1",
|
||||
@@ -32,21 +32,19 @@
|
||||
"superjson": "1.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.26",
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.44",
|
||||
"@testing-library/dom": "8.13.0",
|
||||
"@testing-library/jest-dom": "5.16.3",
|
||||
"@testing-library/react": "13.0.0",
|
||||
"@testing-library/react-hooks": "7.0.2",
|
||||
"@testing-library/user-event": "13.5.0",
|
||||
"@types/lodash.frompairs": "4.0.6",
|
||||
"@types/node": "17.0.16",
|
||||
"@types/react": "18.0.1",
|
||||
"@types/react-dom": "17.0.14",
|
||||
"@types/testing-library__react-hooks": "4.0.0",
|
||||
"blitz": "2.0.0-alpha.26",
|
||||
"blitz": "2.0.0-alpha.44",
|
||||
"cross-spawn": "7.0.3",
|
||||
"find-up": "4.1.0",
|
||||
"lodash.frompairs": "4.0.1",
|
||||
"next": "12.1.6-canary.17",
|
||||
"react": "18.0.0",
|
||||
"react-dom": "18.0.0",
|
||||
|
||||
@@ -17,6 +17,7 @@ import {Router} from "next/router"
|
||||
|
||||
export * from "./error-boundary"
|
||||
export * from "./error-component"
|
||||
export * from "./use-params"
|
||||
export {Routes} from ".blitz"
|
||||
|
||||
const compose =
|
||||
@@ -81,7 +82,7 @@ type RedirectAuthenticatedToFnCtx = {
|
||||
type RedirectAuthenticatedToFn = (args: RedirectAuthenticatedToFnCtx) => RedirectAuthenticatedTo
|
||||
export type BlitzPage<P = {}> = React.ComponentType<P> & {
|
||||
getLayout?: (component: JSX.Element) => JSX.Element
|
||||
authenticate?: boolean | {redirectTo?: string}
|
||||
authenticate?: boolean | {redirectTo?: string | RouteUrlObject}
|
||||
suppressFirstRenderFlicker?: boolean
|
||||
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type {NextConfig} from "next"
|
||||
import {
|
||||
GetServerSideProps,
|
||||
GetServerSidePropsResult,
|
||||
@@ -7,21 +8,21 @@ import {
|
||||
NextApiResponse,
|
||||
} from "next"
|
||||
import type {
|
||||
Ctx as BlitzCtx,
|
||||
BlitzServerPlugin,
|
||||
Middleware,
|
||||
MiddlewareResponse,
|
||||
AsyncFunc,
|
||||
FirstParam,
|
||||
AddParameters,
|
||||
AsyncFunc,
|
||||
BlitzServerPlugin,
|
||||
Ctx as BlitzCtx,
|
||||
FirstParam,
|
||||
RequestMiddleware,
|
||||
MiddlewareResponse,
|
||||
} from "blitz"
|
||||
import {handleRequestWithMiddleware} from "blitz"
|
||||
import type {NextConfig} from "next"
|
||||
import {getQueryKey, getInfiniteQueryKey, installWebpackConfig} from "@blitzjs/rpc"
|
||||
import {dehydrate} from "@blitzjs/rpc"
|
||||
import {handleRequestWithMiddleware, startWatcher, stopWatcher} from "blitz"
|
||||
import {dehydrate, getQueryKey, getInfiniteQueryKey, loaderClient, loaderServer} from "@blitzjs/rpc"
|
||||
import {DefaultOptions, QueryClient} from "react-query"
|
||||
import {IncomingMessage, ServerResponse} from "http"
|
||||
import {withSuperJsonProps} from "./superjson"
|
||||
import {ResolverBasePath} from "@blitzjs/rpc/src/index-server"
|
||||
import {ParsedUrlQuery} from "querystring"
|
||||
|
||||
export * from "./index-browser"
|
||||
|
||||
@@ -38,15 +39,17 @@ export type NextApiHandler = (
|
||||
) => void | Promise<void>
|
||||
|
||||
type SetupBlitzOptions = {
|
||||
plugins: BlitzServerPlugin<Middleware, Ctx>[]
|
||||
plugins: BlitzServerPlugin<RequestMiddleware, Ctx>[]
|
||||
}
|
||||
|
||||
export type BlitzGSSPHandler<TProps> = ({
|
||||
export type BlitzGSSPHandler<TProps, Query extends ParsedUrlQuery = ParsedUrlQuery> = ({
|
||||
ctx,
|
||||
req,
|
||||
res,
|
||||
...args
|
||||
}: Parameters<GetServerSideProps<TProps>>[0] & {ctx: Ctx}) => ReturnType<GetServerSideProps<TProps>>
|
||||
}: Parameters<GetServerSideProps<TProps>>[0] & {ctx: Ctx}) => ReturnType<
|
||||
GetServerSideProps<TProps, Query>
|
||||
>
|
||||
|
||||
export type BlitzGSPHandler<TProps> = ({
|
||||
ctx,
|
||||
@@ -60,11 +63,13 @@ export type BlitzAPIHandler = (
|
||||
) => ReturnType<NextApiHandler>
|
||||
|
||||
export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
|
||||
const middlewares = plugins.flatMap((p) => p.middlewares)
|
||||
const middlewares = plugins.flatMap((p) => p.requestMiddlewares)
|
||||
const contextMiddleware = plugins.flatMap((p) => p.contextMiddleware).filter(Boolean)
|
||||
|
||||
const gSSP =
|
||||
<TProps>(handler: BlitzGSSPHandler<TProps>): GetServerSideProps<TProps> =>
|
||||
<TProps, Query extends ParsedUrlQuery = ParsedUrlQuery>(
|
||||
handler: BlitzGSSPHandler<TProps, Query>,
|
||||
): GetServerSideProps<TProps> =>
|
||||
async ({req, res, ...rest}) => {
|
||||
await handleRequestWithMiddleware<IncomingMessage, ServerResponse>(req, res, middlewares)
|
||||
const ctx = contextMiddleware.reduceRight(
|
||||
@@ -133,22 +138,88 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
|
||||
|
||||
export interface BlitzConfig extends NextConfig {
|
||||
blitz?: {
|
||||
resolverBasePath?: ResolverBasePath
|
||||
customServer?: {
|
||||
hotReload?: boolean
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface WebpackRuleOptions {
|
||||
resolverBasePath?: ResolverBasePath
|
||||
}
|
||||
|
||||
interface WebpackRule {
|
||||
test: RegExp
|
||||
use: Array<{
|
||||
loader: string
|
||||
options: WebpackRuleOptions
|
||||
}>
|
||||
}
|
||||
|
||||
interface InstallWebpackConfigOptions {
|
||||
webpackConfig: {
|
||||
module: {
|
||||
rules: WebpackRule[]
|
||||
}
|
||||
}
|
||||
nextConfig: BlitzConfig
|
||||
}
|
||||
|
||||
export function installWebpackConfig({webpackConfig, nextConfig}: InstallWebpackConfigOptions) {
|
||||
const options: WebpackRuleOptions = {
|
||||
resolverBasePath: nextConfig.blitz?.resolverBasePath,
|
||||
}
|
||||
|
||||
webpackConfig.module.rules.push({
|
||||
test: /\/\[\[\.\.\.blitz]]\.[jt]s$/,
|
||||
use: [
|
||||
{
|
||||
loader: loaderServer,
|
||||
options,
|
||||
},
|
||||
],
|
||||
})
|
||||
webpackConfig.module.rules.push({
|
||||
test: /[\\/](queries|mutations)[\\/]/,
|
||||
use: [
|
||||
{
|
||||
loader: loaderClient,
|
||||
options,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
export function withBlitz(nextConfig: BlitzConfig = {}) {
|
||||
return Object.assign({}, nextConfig, {
|
||||
webpack: (config: any, options: any) => {
|
||||
installWebpackConfig(config)
|
||||
if (
|
||||
process.env.NODE_ENV !== "production" &&
|
||||
process.env.NODE_ENV !== "test" &&
|
||||
process.env.MODE !== "test"
|
||||
) {
|
||||
void startWatcher()
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
void stopWatcher()
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
process.on("exit", function () {
|
||||
void stopWatcher()
|
||||
})
|
||||
}
|
||||
|
||||
const config = Object.assign({}, nextConfig, {
|
||||
webpack: (config: InstallWebpackConfigOptions["webpackConfig"], options: any) => {
|
||||
installWebpackConfig({webpackConfig: config, nextConfig})
|
||||
if (typeof nextConfig.webpack === "function") {
|
||||
return nextConfig.webpack(config, options)
|
||||
}
|
||||
return config
|
||||
},
|
||||
} as NextConfig)
|
||||
})
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
export type PrefetchQueryFn = <T extends AsyncFunc, TInput = FirstParam<T>>(
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
/**
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
|
||||
import React from "react"
|
||||
import {describe, it, expect, vi} from "vitest"
|
||||
import {describe, it, expect, vi, afterEach} from "vitest"
|
||||
import {extractRouterParams, useParam, useParams} from "./use-params"
|
||||
import {renderHook as defaultRenderHook} from "@testing-library/react-hooks"
|
||||
import {NextRouter} from "next/router"
|
||||
@@ -16,6 +15,14 @@ type RenderHookOptions = DefaultHookParams[1] & {
|
||||
dehydratedState?: unknown
|
||||
}
|
||||
|
||||
// This is the router query object which includes route params
|
||||
const query = {
|
||||
id: "1",
|
||||
cat: "category",
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: "",
|
||||
}
|
||||
|
||||
const mockRouter: NextRouter = {
|
||||
basePath: "",
|
||||
pathname: "/",
|
||||
@@ -54,6 +61,10 @@ export function renderHook(
|
||||
}
|
||||
|
||||
describe("extractRouterParams", () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it("returns proper params", () => {
|
||||
const routerQuery = {
|
||||
id: "1",
|
||||
@@ -81,92 +92,99 @@ describe("extractRouterParams", () => {
|
||||
})
|
||||
|
||||
describe("useParams", () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it("works without parameter", () => {
|
||||
// This is the router query object which includes route params
|
||||
const query = {
|
||||
id: "1",
|
||||
cat: "category",
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: "",
|
||||
}
|
||||
|
||||
const {result} = renderHook(() => useParams(), {router: {query}})
|
||||
expect(result.current).toEqual({
|
||||
id: "1",
|
||||
cat: "category",
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: "",
|
||||
vi.mock("next/router", () => {
|
||||
return {
|
||||
useRouter: vi.fn(() => ({...mockRouter, query})),
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it("works with string", () => {
|
||||
// This is the router query object which includes route params
|
||||
const query = {
|
||||
id: "1",
|
||||
cat: "category",
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: "",
|
||||
}
|
||||
it("works with string", () => {
|
||||
vi.mock("next/router", () => {
|
||||
return {
|
||||
useRouter: vi.fn(() => ({...mockRouter, query})),
|
||||
}
|
||||
})
|
||||
|
||||
const {result} = renderHook(() => useParams("string"), {
|
||||
router: {query},
|
||||
const {result} = renderHook(() => useParams("string"), {
|
||||
router: {query},
|
||||
})
|
||||
expect(result.current).toEqual({
|
||||
id: "1",
|
||||
cat: "category",
|
||||
empty: "",
|
||||
})
|
||||
})
|
||||
expect(result.current).toEqual({
|
||||
id: "1",
|
||||
cat: "category",
|
||||
empty: "",
|
||||
})
|
||||
})
|
||||
|
||||
it("works with number", () => {
|
||||
// This is the router query object which includes route params
|
||||
const query = {
|
||||
id: "1",
|
||||
cat: "category",
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: "",
|
||||
}
|
||||
it("works with string", () => {
|
||||
vi.mock("next/router", () => {
|
||||
return {
|
||||
useRouter: vi.fn(() => ({...mockRouter, query})),
|
||||
}
|
||||
})
|
||||
|
||||
const {result} = renderHook(() => useParams("number"), {
|
||||
router: {query},
|
||||
const {result} = renderHook(() => useParams("string"), {
|
||||
router: {query},
|
||||
})
|
||||
expect(result.current).toEqual({
|
||||
id: "1",
|
||||
cat: "category",
|
||||
empty: "",
|
||||
})
|
||||
})
|
||||
expect(result.current).toEqual({
|
||||
id: 1,
|
||||
cat: undefined,
|
||||
slug: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
it("works with array", () => {
|
||||
// This is the router query object which includes route params
|
||||
const query = {
|
||||
id: "1",
|
||||
cat: "category",
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: "",
|
||||
}
|
||||
it("works with number", () => {
|
||||
vi.mock("next/router", () => {
|
||||
return {
|
||||
useRouter: vi.fn(() => ({...mockRouter, query})),
|
||||
}
|
||||
})
|
||||
|
||||
const {result} = renderHook(() => useParams("array"), {
|
||||
router: {query},
|
||||
const {result} = renderHook(() => useParams("number"), {
|
||||
router: {query},
|
||||
})
|
||||
expect(result.current).toEqual({
|
||||
id: 1,
|
||||
cat: undefined,
|
||||
slug: undefined,
|
||||
})
|
||||
})
|
||||
expect(result.current).toEqual({
|
||||
id: ["1"],
|
||||
cat: ["category"],
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: [""],
|
||||
|
||||
it("works with array", () => {
|
||||
vi.mock("next/router", () => {
|
||||
return {
|
||||
useRouter: vi.fn(() => ({...mockRouter, query})),
|
||||
}
|
||||
})
|
||||
|
||||
const {result} = renderHook(() => useParams("array"), {
|
||||
router: {query},
|
||||
})
|
||||
expect(result.current).toEqual({
|
||||
id: ["1"],
|
||||
cat: ["category"],
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: [""],
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("useParam", () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it("works without parameter", () => {
|
||||
// This is the router query object which includes route params
|
||||
const query = {
|
||||
id: "1",
|
||||
cat: "category",
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: "",
|
||||
}
|
||||
vi.mock("next/router", () => {
|
||||
return {
|
||||
useRouter: vi.fn(() => ({...mockRouter, query})),
|
||||
}
|
||||
})
|
||||
|
||||
let {result} = renderHook(() => useParam("id"), {router: {query}})
|
||||
expect(result.current).toEqual("1")
|
||||
@@ -183,13 +201,11 @@ describe("useParam", () => {
|
||||
})
|
||||
|
||||
it("works with string", () => {
|
||||
// This is the router query object which includes route params
|
||||
const query = {
|
||||
id: "1",
|
||||
cat: "category",
|
||||
slug: ["example", "multiple", "slugs"],
|
||||
empty: "",
|
||||
}
|
||||
vi.mock("next/router", () => {
|
||||
return {
|
||||
useRouter: vi.fn(() => ({...mockRouter, query})),
|
||||
}
|
||||
})
|
||||
|
||||
let {result} = renderHook(() => useParam("id", "string"), {
|
||||
router: {query},
|
||||
|
||||
@@ -1,22 +1,10 @@
|
||||
import fromPairs from "lodash.frompairs"
|
||||
import {NextRouter} from "next/router"
|
||||
import {useRouter} from "next/router"
|
||||
import {ParsedUrlQuery} from "querystring"
|
||||
import React from "react"
|
||||
import {RouterContext} from "./router-context"
|
||||
|
||||
type Dict<T> = Record<string, T | undefined>
|
||||
type ReturnTypes = "string" | "number" | "array"
|
||||
|
||||
/**
|
||||
* `useRouter` is a React hook used to access `router` object within components
|
||||
*
|
||||
* @returns `router` object
|
||||
* @see Docs {@link https://blitzjs.com/docs/router#router-object | router}
|
||||
*/
|
||||
export function useRouter(): NextRouter {
|
||||
return React.useContext(RouterContext)
|
||||
}
|
||||
|
||||
/*
|
||||
* Based on the code of https://github.com/lukeed/qss
|
||||
*/
|
||||
@@ -81,7 +69,7 @@ function areQueryValuesEqual(value1: ParsedUrlQueryValue, value2: ParsedUrlQuery
|
||||
}
|
||||
|
||||
export function extractRouterParams(routerQuery: ParsedUrlQuery, asPathQuery: ParsedUrlQuery) {
|
||||
return fromPairs(
|
||||
return Object.fromEntries(
|
||||
Object.entries(routerQuery).filter(
|
||||
([key, value]) =>
|
||||
typeof asPathQuery[key] === "undefined" || !areQueryValuesEqual(value, asPathQuery[key]),
|
||||
|
||||
@@ -1,5 +1,142 @@
|
||||
# @blitzjs/rpc
|
||||
|
||||
## 2.0.0-alpha.44
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [7817fe3a]
|
||||
- @blitzjs/auth@2.0.0-alpha.44
|
||||
- blitz@2.0.0-alpha.44
|
||||
|
||||
## 2.0.0-alpha.43
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [527e48ac]
|
||||
- blitz@2.0.0-alpha.43
|
||||
- @blitzjs/auth@2.0.0-alpha.43
|
||||
|
||||
## 2.0.0-alpha.42
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/auth@2.0.0-alpha.42
|
||||
- blitz@2.0.0-alpha.42
|
||||
|
||||
## 2.0.0-alpha.41
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/auth@2.0.0-alpha.41
|
||||
- blitz@2.0.0-alpha.41
|
||||
|
||||
## 2.0.0-alpha.40
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/auth@2.0.0-alpha.40
|
||||
- blitz@2.0.0-alpha.40
|
||||
|
||||
## 2.0.0-alpha.39
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [b918055b]
|
||||
- blitz@2.0.0-alpha.39
|
||||
- @blitzjs/auth@2.0.0-alpha.39
|
||||
|
||||
## 2.0.0-alpha.38
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 8aee25c5: getQueryClient function & queryClient codemod updates & shared plugin config
|
||||
- blitz@2.0.0-alpha.38
|
||||
- @blitzjs/auth@2.0.0-alpha.38
|
||||
|
||||
## 2.0.0-alpha.37
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [a80d2a8f]
|
||||
- blitz@2.0.0-alpha.37
|
||||
- @blitzjs/auth@2.0.0-alpha.37
|
||||
|
||||
## 2.0.0-alpha.36
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 4cad9cca: Add queryClient to RPC Plugin exports
|
||||
- blitz@2.0.0-alpha.36
|
||||
- @blitzjs/auth@2.0.0-alpha.36
|
||||
|
||||
## 2.0.0-alpha.35
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.35
|
||||
- @blitzjs/auth@2.0.0-alpha.35
|
||||
|
||||
## 2.0.0-alpha.34
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- dfd2408e: Add resolverBasePath to Blitz config to change the way rpc routes are generated
|
||||
- @blitzjs/auth@2.0.0-alpha.34
|
||||
- blitz@2.0.0-alpha.34
|
||||
|
||||
## 2.0.0-alpha.33
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/auth@2.0.0-alpha.33
|
||||
- blitz@2.0.0-alpha.33
|
||||
|
||||
## 2.0.0-alpha.32
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @blitzjs/auth@2.0.0-alpha.32
|
||||
- blitz@2.0.0-alpha.32
|
||||
|
||||
## 2.0.0-alpha.31
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 17ce29e5: Update RPC plugin setup in templates
|
||||
- blitz@2.0.0-alpha.31
|
||||
- @blitzjs/auth@2.0.0-alpha.31
|
||||
|
||||
## 2.0.0-alpha.30
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [ce453683]
|
||||
- blitz@2.0.0-alpha.30
|
||||
- @blitzjs/auth@2.0.0-alpha.30
|
||||
|
||||
## 2.0.0-alpha.29
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [962eb58a]
|
||||
- blitz@2.0.0-alpha.29
|
||||
- @blitzjs/auth@2.0.0-alpha.29
|
||||
|
||||
## 2.0.0-alpha.28
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.28
|
||||
- @blitzjs/auth@2.0.0-alpha.28
|
||||
|
||||
## 2.0.0-alpha.27
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 07292910: Add invokeWithCtx function
|
||||
- @blitzjs/auth@2.0.0-alpha.27
|
||||
- blitz@2.0.0-alpha.27
|
||||
|
||||
## 2.0.0-alpha.26
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/rpc",
|
||||
"version": "2.0.0-alpha.26",
|
||||
"version": "2.0.0-alpha.44",
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts && wait-on -d 250 ../blitz-auth/dist/index-browser.d.ts",
|
||||
@@ -20,7 +20,7 @@
|
||||
"dist/**"
|
||||
],
|
||||
"dependencies": {
|
||||
"@blitzjs/auth": "2.0.0-alpha.26",
|
||||
"@blitzjs/auth": "2.0.0-alpha.44",
|
||||
"b64-lite": "1.4.0",
|
||||
"bad-behavior": "1.0.1",
|
||||
"chalk": "^4.1.0",
|
||||
@@ -30,11 +30,11 @@
|
||||
"zod": "3.10.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.26",
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.44",
|
||||
"@types/debug": "4.1.7",
|
||||
"@types/react": "18.0.1",
|
||||
"@types/react-dom": "17.0.14",
|
||||
"blitz": "2.0.0-alpha.26",
|
||||
"blitz": "2.0.0-alpha.44",
|
||||
"next": "12.1.6-canary.17",
|
||||
"react": "18.0.0",
|
||||
"react-dom": "18.0.0",
|
||||
@@ -43,7 +43,7 @@
|
||||
"watch": "1.0.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"blitz": "2.0.0-alpha.26",
|
||||
"blitz": "2.0.0-alpha.44",
|
||||
"next": "*"
|
||||
},
|
||||
"publishConfig": {
|
||||
|
||||
@@ -2,12 +2,13 @@ export * from "./rpc"
|
||||
export {useQuery, usePaginatedQuery, useInfiniteQuery, useMutation} from "./react-query"
|
||||
export type {MutateFunction} from "./react-query"
|
||||
export {
|
||||
queryClient,
|
||||
getQueryKey,
|
||||
getInfiniteQueryKey,
|
||||
invalidateQuery,
|
||||
setQueryData,
|
||||
getQueryClient,
|
||||
} from "./react-query-utils"
|
||||
export {useQueryErrorResetBoundary, QueryClient} from "react-query"
|
||||
export {dehydrate} from "react-query/hydration"
|
||||
export {invoke} from "./invoke"
|
||||
export {invokeWithCtx} from "./invokeWithCtx"
|
||||
|
||||
15
packages/blitz-rpc/src/data-client/invokeWithCtx.ts
Normal file
15
packages/blitz-rpc/src/data-client/invokeWithCtx.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import {FirstParam, PromiseReturnType, Ctx} from "blitz"
|
||||
|
||||
export function invokeWithCtx<T extends (...args: any) => any, TInput = FirstParam<T>>(
|
||||
queryFn: T,
|
||||
params: TInput,
|
||||
ctx: Ctx,
|
||||
): Promise<PromiseReturnType<T>> {
|
||||
if (typeof queryFn === "undefined") {
|
||||
throw new Error(
|
||||
"invokeWithCtx is missing the first argument - it must be a query or mutation function",
|
||||
)
|
||||
}
|
||||
|
||||
return queryFn(params, ctx)
|
||||
}
|
||||
@@ -54,8 +54,8 @@ export const initializeQueryClient = () => {
|
||||
})
|
||||
}
|
||||
|
||||
// Create internal QueryClient instance
|
||||
export const queryClient = initializeQueryClient()
|
||||
// Query client is initialised in `BlitzRpcPlugin`, and can only be used with BlitzRpcPlugin right now
|
||||
export const getQueryClient = () => globalThis.queryClient
|
||||
|
||||
function isRpcClient(f: any): f is RpcClient<any, any> {
|
||||
return !!f._isRpcClient
|
||||
@@ -176,7 +176,7 @@ export function invalidateQuery<TInput, TResult, T extends AsyncFunc>(
|
||||
// Params not provided, only use first query key item (url)
|
||||
queryKey = fullQueryKey[0]
|
||||
}
|
||||
return queryClient.invalidateQueries(queryKey)
|
||||
return getQueryClient().invalidateQueries(queryKey)
|
||||
}
|
||||
|
||||
export function setQueryData<TInput, TResult, T extends AsyncFunc>(
|
||||
@@ -184,15 +184,15 @@ export function setQueryData<TInput, TResult, T extends AsyncFunc>(
|
||||
params: TInput,
|
||||
newData: TResult | ((oldData: TResult | undefined) => TResult),
|
||||
opts: MutateOptions = {refetch: true},
|
||||
): Promise<void | ReturnType<typeof queryClient.invalidateQueries>> {
|
||||
): Promise<void | ReturnType<ReturnType<typeof getQueryClient>["invalidateQueries"]>> {
|
||||
if (typeof resolver === "undefined") {
|
||||
throw new Error("setQueryData is missing the first argument - it must be a resolver function")
|
||||
}
|
||||
const queryKey = getQueryKey(resolver, params)
|
||||
|
||||
return new Promise((res) => {
|
||||
queryClient.setQueryData(queryKey, newData)
|
||||
let result: void | ReturnType<typeof queryClient.invalidateQueries>
|
||||
getQueryClient().setQueryData(queryKey, newData)
|
||||
let result: void | ReturnType<ReturnType<typeof getQueryClient>["invalidateQueries"]>
|
||||
if (opts.refetch) {
|
||||
result = invalidateQuery(resolver, params)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ import {normalizePathTrailingSlash} from "next/dist/client/normalize-trailing-sl
|
||||
import {addBasePath} from "next/dist/shared/lib/router/router"
|
||||
import {deserialize, serialize} from "superjson"
|
||||
import {SuperJSONResult} from "superjson/dist/types"
|
||||
import {isServer, CSRFTokenMismatchError} from "blitz"
|
||||
import {getQueryKeyFromUrlAndParams, queryClient} from "./react-query-utils"
|
||||
import {CSRFTokenMismatchError, isServer} from "blitz"
|
||||
import {getQueryKeyFromUrlAndParams, getQueryClient} from "./react-query-utils"
|
||||
import {
|
||||
getAntiCSRFToken,
|
||||
getPublicDataStore,
|
||||
@@ -55,7 +55,7 @@ export function __internal_buildRpcClient({
|
||||
resolverType,
|
||||
routePath,
|
||||
}: BuildRpcClientParams): RpcClient {
|
||||
const fullRoutePath = normalizeApiRoute("/api/rpc/" + routePath)
|
||||
const fullRoutePath = normalizeApiRoute("/api/rpc" + routePath)
|
||||
|
||||
const httpClient: RpcClientBase = async (params, opts = {}) => {
|
||||
const debug = (await import("debug")).default("blitz:rpc")
|
||||
@@ -123,9 +123,9 @@ export function __internal_buildRpcClient({
|
||||
setTimeout(async () => {
|
||||
// Do these in the next tick to prevent various bugs like https://github.com/blitz-js/blitz/issues/2207
|
||||
debug("Invalidating react-query cache...")
|
||||
await queryClient.cancelQueries()
|
||||
await queryClient.resetQueries()
|
||||
queryClient.getMutationCache().clear()
|
||||
await getQueryClient().cancelQueries()
|
||||
await getQueryClient().resetQueries()
|
||||
getQueryClient().getMutationCache().clear()
|
||||
// We have a 100ms delay here to prevent unnecessary stale queries from running
|
||||
// This prevents the case where you logout on a page with
|
||||
// Page.authenticate = {redirectTo: '/login'}
|
||||
@@ -184,7 +184,7 @@ export function __internal_buildRpcClient({
|
||||
|
||||
if (!opts.fromQueryHook) {
|
||||
const queryKey = getQueryKeyFromUrlAndParams(routePath, params)
|
||||
queryClient.setQueryData(queryKey, data)
|
||||
getQueryClient().setQueryData(queryKey, data)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user