Unverified Commit 59714db1 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update server-only changes HMR handling (#34298)

* Update server-only changes HMR handling

* Add failing tests for GS(S)P server only changes

* update test

* normalize backslashes

* Update to xor the chunk hashes

* remove test change

* remove other test change
parent d288d43f
...@@ -3,7 +3,7 @@ import { IncomingMessage, ServerResponse } from 'http' ...@@ -3,7 +3,7 @@ import { IncomingMessage, ServerResponse } from 'http'
import { WebpackHotMiddleware } from './hot-middleware' import { WebpackHotMiddleware } from './hot-middleware'
import { join, relative, isAbsolute } from 'path' import { join, relative, isAbsolute } from 'path'
import { UrlObject } from 'url' import { UrlObject } from 'url'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack, StringXor } from 'next/dist/compiled/webpack/webpack'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack' import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import { import {
createEntrypoints, createEntrypoints,
...@@ -594,21 +594,56 @@ export default class HotReloader { ...@@ -594,21 +594,56 @@ export default class HotReloader {
const trackPageChanges = const trackPageChanges =
(pageHashMap: Map<string, string>, changedItems: Set<string>) => (pageHashMap: Map<string, string>, changedItems: Set<string>) =>
(stats: webpack5.Compilation) => { (stats: webpack5.Compilation) => {
stats.entrypoints.forEach((entry, key) => { try {
if (key.startsWith('pages/')) { stats.entrypoints.forEach((entry, key) => {
// TODO this doesn't handle on demand loaded chunks if (key.startsWith('pages/')) {
entry.chunks.forEach((chunk: any) => { // TODO this doesn't handle on demand loaded chunks
if (chunk.id === key) { entry.chunks.forEach((chunk) => {
const prevHash = pageHashMap.get(key) if (chunk.id === key) {
const modsIterable: any =
if (prevHash && prevHash !== chunk.hash) { stats.chunkGraph.getChunkModulesIterable(chunk)
changedItems.add(key)
let chunksHash = new StringXor()
modsIterable.forEach((mod: any) => {
if (
mod.resource &&
mod.resource.replace(/\\/g, '/').includes(key)
) {
// use original source to calculate hash since mod.hash
// includes the source map in development which changes
// every time for both server and client so we calculate
// the hash without the source map for the page module
const hash = require('crypto')
.createHash('sha256')
.update(mod.originalSource().buffer())
.digest()
.toString('hex')
chunksHash.add(hash)
} else {
// for non-pages we can use the module hash directly
const hash = stats.chunkGraph.getModuleHash(
mod,
chunk.runtime
)
chunksHash.add(hash)
}
})
const prevHash = pageHashMap.get(key)
const curHash = chunksHash.toString()
if (prevHash && prevHash !== curHash) {
changedItems.add(key)
}
pageHashMap.set(key, curHash)
} }
pageHashMap.set(key, chunk.hash) })
} }
}) })
} } catch (err) {
}) console.error(err)
}
} }
multiCompiler.compilers[0].hooks.emit.tap( multiCompiler.compilers[0].hooks.emit.tap(
......
...@@ -35,6 +35,7 @@ declare module 'next/dist/compiled/webpack/webpack' { ...@@ -35,6 +35,7 @@ declare module 'next/dist/compiled/webpack/webpack' {
export let BasicEvaluatedExpression: any export let BasicEvaluatedExpression: any
export let GraphHelpers: any export let GraphHelpers: any
export let sources: typeof webpackSources export let sources: typeof webpackSources
export let StringXor: any
// TODO change this to webpack5 // TODO change this to webpack5
export { webpack4 as webpack, loader, webpack4, webpack5 } export { webpack4 as webpack, loader, webpack4, webpack5 }
} }
......
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import data from '../lib/data.json'
export default function Gsp(props) { export default function Gsp(props) {
if (useRouter().isFallback) { if (useRouter().isFallback) {
...@@ -19,6 +20,7 @@ export const getStaticProps = async () => { ...@@ -19,6 +20,7 @@ export const getStaticProps = async () => {
return { return {
props: { props: {
count, count,
data,
random: Math.random(), random: Math.random(),
}, },
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment