Skip to content

Commit 53e13ea

Browse files
author
Lucas Castro
authored
Merge branch 'main' into fix-react-prop-typo
2 parents 390dc03 + 4969761 commit 53e13ea

27 files changed

+2092
-2016
lines changed

.swcrc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"env": {
3+
"targets": {
4+
"node": 16
5+
}
6+
},
7+
"jsc": {
8+
"parser": {
9+
"syntax": "typescript",
10+
"decorators": true,
11+
"dynamicImport": true
12+
},
13+
"target": "es2020"
14+
},
15+
"module": {
16+
"type": "commonjs"
17+
}
18+
}

jest.config.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
module.exports = {
2-
preset: 'ts-jest',
3-
testTimeout: process.env.CI ? 30000 : 10000,
4-
globalSetup: './scripts/jestGlobalSetup.js',
5-
globalTeardown: './scripts/jestGlobalTeardown.js',
6-
testEnvironment: './scripts/jestEnv.js',
2+
projects: ['<rootDir>/packages/fastify-renderer', '<rootDir>/packages/test-apps/simple-react'],
73
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"devDependencies": {
4949
"cross-env": "^7.0.3",
5050
"fs-extra": "^10.1.0",
51-
"playwright-chromium": "^1.21.0",
51+
"playwright-chromium": "^1.25.0",
5252
"wds": "^0.12.0"
5353
}
5454
}

packages/fastify-renderer/jest.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module.exports = {
9393
// notifyMode: "failure-change",
9494

9595
// A preset that is used as a base for Jest's configuration
96-
preset: 'ts-jest',
96+
// preset: 'ts-jest',
9797

9898
// Run tests from one or more projects
9999
// projects: undefined,
@@ -172,7 +172,9 @@ module.exports = {
172172
// timers: "real",
173173

174174
// A map from regular expressions to paths to transformers
175-
// transform: undefined,
175+
transform: {
176+
'^.+\\.(t|j)sx?$': '@swc/jest',
177+
},
176178

177179
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
178180
// transformIgnorePatterns: [

packages/fastify-renderer/package.json

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,52 +57,53 @@
5757
"node": ">= 12.0.0"
5858
},
5959
"dependencies": {
60+
"@fastify/accepts": "^4.0.0",
61+
"@fastify/middie": "^8.0.0",
62+
"@fastify/static": "^6.4.0",
6063
"@opentelemetry/api": "^1.1.0",
6164
"@vitejs/plugin-react-refresh": "^1.3.6",
62-
"fastify-accepts": "^2.1.0",
63-
"fastify-express": "^0.3.3",
6465
"fastify-plugin": "^3.0.1",
65-
"fastify-static": "^4.6.1",
6666
"http-errors": "^1.8.1",
67-
"middie": "^5.4.0",
6867
"path-to-regexp": "^6.2.1",
6968
"sanitize-filename": "^1.6.3",
7069
"stream-template": "^0.0.10",
71-
"vite": "^2.9.13",
70+
"vite": "^2.9.15",
7271
"wouter": "^2.7.5"
7372
},
7473
"peerDependencies": {
75-
"fastify": "^3.13.0",
74+
"fastify": "^4.2.0",
7675
"react": "experimental",
7776
"react-dom": "experimental"
7877
},
7978
"devDependencies": {
79+
"@swc/core": "^1.3.9",
80+
"@swc/jest": "^0.2.22",
8081
"@types/connect": "^3.4.35",
8182
"@types/jest": "^26.0.24",
8283
"@types/node": "^14.17.20",
8384
"@types/react": "^17.0.43",
8485
"@types/react-dom": "^17.0.11",
8586
"@types/sanitize-filename": "^1.6.3",
86-
"@typescript-eslint/eslint-plugin": "^5.30.0",
87-
"@typescript-eslint/parser": "^5.30.0",
87+
"@typescript-eslint/eslint-plugin": "^5.40.0",
88+
"@typescript-eslint/parser": "^5.40.0",
8889
"eslint": "^7.32.0",
8990
"eslint-config-prettier": "^8.5.0",
9091
"eslint-plugin-import": "^2.26.0",
9192
"eslint-plugin-prettier": "^3.4.1",
92-
"eslint-plugin-react": "^7.30.1",
93+
"eslint-plugin-react": "^7.31.7",
9394
"eslint-plugin-react-hooks": "^4.6.0",
94-
"fastify": "^3.29.0",
95+
"fastify": "^4.2.0",
9596
"gitpkg": "^1.0.0-beta.2",
96-
"jest": "^27.5.1",
97+
"jest": "^28.1.3",
98+
"jest-playwright-preset": "^2.0.0",
9799
"npm-run-all": "^4.1.5",
98-
"pino-pretty": "^4.8.0",
100+
"pino-pretty": "^8.1.0",
99101
"prettier": "^2.7.1",
100102
"prettier-plugin-organize-imports": "^2.3.4",
101103
"react": "0.0.0-experimental-4ead6b530",
102104
"react-dom": "0.0.0-experimental-4ead6b530",
103105
"rimraf": "^3.0.2",
104-
"ts-jest": "^27.1.4",
105-
"typescript": "^4.6.3"
106+
"typescript": "^4.7.4"
106107
},
107108
"files": [
108109
"client",

packages/fastify-renderer/src/client/react/Root.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from 'react'
2-
import { Route, Router, Switch, useLocation } from 'wouter'
2+
import { Route, Router, Switch, useLocation, useRouter } from 'wouter'
33
import { usePromise } from './fetcher'
4-
import { useNavigationDetails, useTransitionLocation } from './locationHook'
4+
import { shouldScrollToHash, useNavigationDetails, useTransitionLocation } from './locationHook'
55
import { matcher } from './matcher'
66

77
export interface LayoutProps {
@@ -47,6 +47,7 @@ export function Root<BootProps>(props: {
4747
<Route path={route} key={route}>
4848
{(params) => {
4949
const [location] = useLocation()
50+
const router = useRouter()
5051
const backendPath = location.split('#')[0] // remove current anchor for fetching data from the server side
5152

5253
const payload = usePromise<{ props: Record<string, any> }>(props.basePath + backendPath, async () => {
@@ -65,9 +66,11 @@ export function Root<BootProps>(props: {
6566
}
6667
})
6768

68-
// navigate to the anchor in the url after rendering
69+
// Navigate to the anchor in the url after rendering, unless we're using replaceState and
70+
// the destination page and previous page have the same base route (i.e. before '#')
71+
// We would do this for example to update the url to the correct anchor as the user scrolls.
6972
useEffect(() => {
70-
if (window.location.hash) {
73+
if (window.location.hash && shouldScrollToHash(router.navigationHistory)) {
7174
document.getElementById(window.location.hash.slice(1))?.scrollIntoView()
7275
}
7376
}, [location])

packages/fastify-renderer/src/client/react/locationHook.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { unstable_useTransition as useTransition, useCallback, useEffect, useRef, useState } from 'react'
2-
import { useLocation } from 'wouter'
2+
import { NavigationHistory, useLocation, useRouter } from 'wouter'
33

44
/**
55
* History API docs @see https://developer.mozilla.org/en-US/docs/Web/API/History
@@ -20,6 +20,16 @@ export const useTransitionLocation = ({ base = '' } = {}) => {
2020
const [path, update] = useState(() => currentPathname(base)) // @see https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
2121
const prevLocation = useRef(path + location.search + location.hash)
2222
const [startTransition, isPending] = useTransition()
23+
const router = useRouter()
24+
useEffect(() => {
25+
if (!router.navigationHistory)
26+
router.navigationHistory = {
27+
current: {
28+
path,
29+
replace: false,
30+
},
31+
}
32+
}, [])
2333

2434
useEffect(() => {
2535
// this function checks if the location has been changed since the
@@ -31,9 +41,13 @@ export const useTransitionLocation = ({ base = '' } = {}) => {
3141

3242
if (prevLocation.current !== destination) {
3343
prevLocation.current = destination
34-
startTransition(() => {
44+
if (shouldScrollToHash(router.navigationHistory)) {
45+
startTransition(() => {
46+
update(destination)
47+
})
48+
} else {
3549
update(destination)
36-
})
50+
}
3751
}
3852
}
3953

@@ -61,11 +75,23 @@ export const useTransitionLocation = ({ base = '' } = {}) => {
6175
return
6276
}
6377

78+
const path = base + to
79+
80+
if (!router.navigationHistory) router.navigationHistory = {}
81+
if (router.navigationHistory?.current) {
82+
router.navigationHistory.previous = { ...router.navigationHistory.current }
83+
}
84+
85+
router.navigationHistory.current = {
86+
path,
87+
replace,
88+
}
89+
6490
history[replace ? eventReplaceState : eventPushState](
6591
null,
6692
'',
6793
// handle nested routers and absolute paths
68-
base + to
94+
path
6995
)
7096
},
7197
[base]
@@ -105,3 +131,16 @@ export const useNavigationDetails = (): [boolean, string] => {
105131

106132
const currentPathname = (base, path = location.pathname + location.search + location.hash) =>
107133
!path.toLowerCase().indexOf(base.toLowerCase()) ? path.slice(base.length) || '/' : '~' + path
134+
135+
export const navigatingOnSamePage = (history?: NavigationHistory): boolean => {
136+
const { current, previous } = history || {}
137+
138+
if (!history) return false
139+
if (!current || !previous) return false
140+
141+
return current.path.split('#')[0] == previous.path.split('#')[0]
142+
}
143+
144+
export const shouldScrollToHash = (history?: NavigationHistory): boolean => {
145+
return !(navigatingOnSamePage(history) && history?.current?.replace)
146+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'wouter'
2+
3+
declare module 'wouter' {
4+
export interface RouterProps {
5+
navigationHistory?: NavigationHistory
6+
}
7+
8+
export interface NavigationHistory {
9+
current?: NavigationHistoryItem
10+
previous?: NavigationHistoryItem
11+
}
12+
13+
export interface NavigationHistoryItem {
14+
path: string
15+
replace: boolean
16+
}
17+
}

packages/fastify-renderer/src/node/Plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/require-await */
2+
import '@fastify/middie'
23
import fs from 'fs'
3-
import 'middie'
44
import path from 'path'
55
import { InlineConfig } from 'vite'
66
import { Template } from './DocumentTemplate'

packages/fastify-renderer/src/node/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable @typescript-eslint/require-await */
2+
import '@fastify/accepts'
3+
import '@fastify/middie'
4+
import fastifyStatic from '@fastify/static'
25
import { FastifyInstance, FastifyReply } from 'fastify'
3-
import 'fastify-accepts'
46
import fp from 'fastify-plugin'
5-
import fastifyStatic from 'fastify-static'
67
import { promises as fs } from 'fs'
7-
import 'middie'
88
import path from 'path'
99
import {
1010
build as viteBuild,
@@ -198,9 +198,9 @@ const FastifyRenderer = fp<FastifyRendererOptions>(
198198
})
199199
},
200200
{
201-
fastify: '3.x',
201+
fastify: '4.x',
202202
name: 'fastify-renderer',
203-
dependencies: ['fastify-accepts', 'middie'],
203+
dependencies: ['@fastify/accepts', 'middie'],
204204
}
205205
)
206206

0 commit comments

Comments
 (0)