|
| 1 | +import http from 'http' |
| 2 | +import httpProxy from 'http-proxy' |
| 3 | +import webdriver from 'next-webdriver' |
| 4 | +import { |
| 5 | + findPort, |
| 6 | + killApp, |
| 7 | + launchApp, |
| 8 | + nextBuild, |
| 9 | + nextStart, |
| 10 | +} from 'next-test-utils' |
| 11 | +import { isNextDev, isNextStart, nextTestSetup } from 'e2e-utils' |
| 12 | + |
| 13 | +const appDir = __dirname |
| 14 | + |
| 15 | +let proxyPort |
| 16 | + |
| 17 | +const runTests = (switchDeployment: (bool) => void) => { |
| 18 | + it('index to gsp', async () => { |
| 19 | + switchDeployment(false) |
| 20 | + const browser = await webdriver(proxyPort, '/') |
| 21 | + |
| 22 | + await browser.eval('window.beforeNav = 1') |
| 23 | + await browser.waitForElementByCss('#index') |
| 24 | + |
| 25 | + switchDeployment(true) |
| 26 | + |
| 27 | + await browser.eval(`(function() { |
| 28 | + window.next.router.push('/gsp') |
| 29 | + })()`) |
| 30 | + await browser.waitForElementByCss('#gsp') |
| 31 | + |
| 32 | + expect(await browser.eval('window.beforeNav')).toBeFalsy() |
| 33 | + }) |
| 34 | + |
| 35 | + it('gsp to gssp', async () => { |
| 36 | + switchDeployment(false) |
| 37 | + const browser = await webdriver(proxyPort, '/gsp') |
| 38 | + |
| 39 | + await browser.eval('window.beforeNav = 1') |
| 40 | + await browser.waitForElementByCss('#gsp') |
| 41 | + |
| 42 | + switchDeployment(true) |
| 43 | + |
| 44 | + await browser.eval(`(function() { |
| 45 | + window.next.router.push('/gssp') |
| 46 | + })()`) |
| 47 | + await browser.waitForElementByCss('#gssp') |
| 48 | + |
| 49 | + expect(await browser.eval('window.beforeNav')).toBeFalsy() |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +describe('pages ssg data deployment skew - hard navigate when a new deployment occurs', () => { |
| 54 | + if (process.platform === 'win32') { |
| 55 | + it('should skip this suite on Windows', () => {}) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if (isNextDev) { |
| 60 | + describe('development mode', () => { |
| 61 | + let should404Data = false |
| 62 | + let apps = [] |
| 63 | + let proxyServer |
| 64 | + |
| 65 | + beforeAll(async () => { |
| 66 | + const appPort = await findPort() |
| 67 | + apps.push(await launchApp(appDir, appPort)) |
| 68 | + |
| 69 | + const proxy = httpProxy.createProxyServer({ |
| 70 | + target: `http://localhost:${appPort}`, |
| 71 | + }) |
| 72 | + proxyPort = await findPort() |
| 73 | + |
| 74 | + proxyServer = http.createServer((req, res) => { |
| 75 | + req.on('error', (e) => { |
| 76 | + require('console').error(e) |
| 77 | + }) |
| 78 | + res.on('error', (e) => { |
| 79 | + require('console').error(e) |
| 80 | + }) |
| 81 | + if (should404Data && req.url.match(/\/_next\/data/)) { |
| 82 | + res.statusCode = 404 |
| 83 | + return res.end('not found') |
| 84 | + } |
| 85 | + proxy.web(req, res) |
| 86 | + }) |
| 87 | + |
| 88 | + await new Promise<void>((resolve) => { |
| 89 | + proxyServer.listen(proxyPort, () => resolve()) |
| 90 | + }) |
| 91 | + }) |
| 92 | + afterAll(async () => { |
| 93 | + for (const app of apps) { |
| 94 | + await killApp(app) |
| 95 | + } |
| 96 | + proxyServer.close() |
| 97 | + }) |
| 98 | + |
| 99 | + runTests((v) => { |
| 100 | + should404Data = v |
| 101 | + }) |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + if (isNextStart) { |
| 106 | + describe.each([ |
| 107 | + { name: 'with build id' }, |
| 108 | + { |
| 109 | + name: 'with deployment id', |
| 110 | + NEXT_DEPLOYMENT_ID1: 'deployment-id-1', |
| 111 | + NEXT_DEPLOYMENT_ID2: 'deployment-id-2', |
| 112 | + }, |
| 113 | + { name: 'with build id (output export)', OUTPUT_MODE: 'export' }, |
| 114 | + ])( |
| 115 | + 'production mode $name', |
| 116 | + ({ NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID2, OUTPUT_MODE }) => { |
| 117 | + let shouldSwitchDeployment = false |
| 118 | + let apps = [] |
| 119 | + let proxyServer |
| 120 | + |
| 121 | + beforeAll(async () => { |
| 122 | + await nextBuild(appDir, [], { |
| 123 | + env: { |
| 124 | + DIST_DIR: '1', |
| 125 | + NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1, |
| 126 | + OUTPUT_MODE, |
| 127 | + }, |
| 128 | + }) |
| 129 | + let appPort1 = await findPort() |
| 130 | + apps.push( |
| 131 | + await nextStart(appDir, appPort1, { |
| 132 | + env: { |
| 133 | + DIST_DIR: '1', |
| 134 | + NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1, |
| 135 | + OUTPUT_MODE, |
| 136 | + }, |
| 137 | + }) |
| 138 | + ) |
| 139 | + |
| 140 | + await nextBuild(appDir, [], { |
| 141 | + env: { |
| 142 | + DIST_DIR: '2', |
| 143 | + NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2, |
| 144 | + }, |
| 145 | + }) |
| 146 | + let appPort2 = await findPort() |
| 147 | + apps.push( |
| 148 | + await nextStart(appDir, appPort2, { |
| 149 | + env: { |
| 150 | + DIST_DIR: '2', |
| 151 | + NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2, |
| 152 | + }, |
| 153 | + }) |
| 154 | + ) |
| 155 | + |
| 156 | + const proxy1 = httpProxy.createProxyServer({ |
| 157 | + target: `http://localhost:${appPort1}`, |
| 158 | + }) |
| 159 | + const proxy2 = httpProxy.createProxyServer({ |
| 160 | + target: `http://localhost:${appPort2}`, |
| 161 | + }) |
| 162 | + proxyPort = await findPort() |
| 163 | + |
| 164 | + proxyServer = http.createServer((req, res) => { |
| 165 | + req.on('error', (e) => { |
| 166 | + require('console').error(e) |
| 167 | + }) |
| 168 | + res.on('error', (e) => { |
| 169 | + require('console').error(e) |
| 170 | + }) |
| 171 | + |
| 172 | + if (shouldSwitchDeployment) { |
| 173 | + proxy2.web(req, res, undefined, (e) => { |
| 174 | + require('console').error(e) |
| 175 | + }) |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + proxy1.web(req, res, undefined, (e) => { |
| 180 | + require('console').error(e) |
| 181 | + }) |
| 182 | + }) |
| 183 | + |
| 184 | + await new Promise<void>((resolve) => { |
| 185 | + proxyServer.listen(proxyPort, () => resolve()) |
| 186 | + }) |
| 187 | + }) |
| 188 | + afterAll(async () => { |
| 189 | + for (const app of apps) { |
| 190 | + await killApp(app) |
| 191 | + } |
| 192 | + proxyServer.close() |
| 193 | + }) |
| 194 | + |
| 195 | + runTests((v) => { |
| 196 | + shouldSwitchDeployment = v |
| 197 | + }) |
| 198 | + } |
| 199 | + ) |
| 200 | + } |
| 201 | + |
| 202 | + describe('header with deployment id', () => { |
| 203 | + const { next } = nextTestSetup({ |
| 204 | + files: appDir, |
| 205 | + }) |
| 206 | + |
| 207 | + // Deployment skew is hard to properly e2e deploy test, so this just checks for the header |
| 208 | + it('header is set on data routes', async () => { |
| 209 | + for (const route of ['/gsp', '/gssp']) { |
| 210 | + await next.fetch(route) |
| 211 | + let res = await next.fetch(`/_next/data${route}.json`) |
| 212 | + |
| 213 | + expect(res.status).toBe(200) |
| 214 | + expect(res.headers.get('content-type')).toStartWith('application/json') |
| 215 | + expect(res.headers.get('x-nextjs-deployment-id')).toBeTruthy() |
| 216 | + } |
| 217 | + }) |
| 218 | + }) |
| 219 | +}) |
0 commit comments