|
1 | 1 | import fs from 'fs-extra' |
| 2 | +import cookie from 'cookie' |
2 | 3 | import cheerio from 'cheerio' |
3 | 4 | import { join, sep } from 'path' |
4 | 5 | import escapeRegex from 'escape-string-regexp' |
@@ -178,6 +179,11 @@ describe('Prerender', () => { |
178 | 179 | initialRevalidateSeconds: 1, |
179 | 180 | srcRoute: null, |
180 | 181 | }, |
| 182 | + '/preview': { |
| 183 | + dataRoute: `/_next/data/${next.buildId}/preview.json`, |
| 184 | + initialRevalidateSeconds: false, |
| 185 | + srcRoute: null, |
| 186 | + }, |
181 | 187 | '/api-docs/first': { |
182 | 188 | dataRoute: `/_next/data/${next.buildId}/api-docs/first.json`, |
183 | 189 | initialRevalidateSeconds: false, |
@@ -1518,6 +1524,14 @@ describe('Prerender', () => { |
1518 | 1524 | p: 'p', |
1519 | 1525 | }, |
1520 | 1526 | }, |
| 1527 | + { |
| 1528 | + dataRouteRegex: normalizeRegEx( |
| 1529 | + `^\\/_next\\/data\\/${escapeRegex( |
| 1530 | + next.buildId |
| 1531 | + )}\\/preview.json$` |
| 1532 | + ), |
| 1533 | + page: '/preview', |
| 1534 | + }, |
1521 | 1535 | { |
1522 | 1536 | dataRouteRegex: normalizeRegEx( |
1523 | 1537 | `^\\/_next\\/data\\/${escapeRegex( |
@@ -1772,6 +1786,67 @@ describe('Prerender', () => { |
1772 | 1786 | }) |
1773 | 1787 | } |
1774 | 1788 |
|
| 1789 | + it('should revalidate manual revalidate with preview cookie', async () => { |
| 1790 | + const initialRes = await fetchViaHTTP(next.url, '/preview') |
| 1791 | + expect(initialRes.status).toBe(200) |
| 1792 | + |
| 1793 | + const initial$ = cheerio.load(await initialRes.text()) |
| 1794 | + const initialProps = JSON.parse(initial$('#props').text()) |
| 1795 | + |
| 1796 | + expect(initialProps).toEqual({ |
| 1797 | + preview: false, |
| 1798 | + previewData: null, |
| 1799 | + }) |
| 1800 | + |
| 1801 | + const previewRes = await fetchViaHTTP(next.url, '/api/enable') |
| 1802 | + let previewCookie = '' |
| 1803 | + |
| 1804 | + expect(previewRes.headers.get('set-cookie')).toMatch( |
| 1805 | + /(__prerender_bypass|__next_preview_data)/ |
| 1806 | + ) |
| 1807 | + |
| 1808 | + previewRes.headers |
| 1809 | + .get('set-cookie') |
| 1810 | + .split(',') |
| 1811 | + .forEach((c) => { |
| 1812 | + c = cookie.parse(c) |
| 1813 | + const isBypass = c.__prerender_bypass |
| 1814 | + |
| 1815 | + if (isBypass || c.__next_preview_data) { |
| 1816 | + if (previewCookie) previewCookie += '; ' |
| 1817 | + |
| 1818 | + previewCookie += `${ |
| 1819 | + isBypass ? '__prerender_bypass' : '__next_preview_data' |
| 1820 | + }=${c[isBypass ? '__prerender_bypass' : '__next_preview_data']}` |
| 1821 | + } |
| 1822 | + }) |
| 1823 | + |
| 1824 | + const apiRes = await fetchViaHTTP( |
| 1825 | + next.url, |
| 1826 | + '/api/manual-revalidate', |
| 1827 | + { pathname: '/preview' }, |
| 1828 | + { |
| 1829 | + headers: { |
| 1830 | + cookie: previewCookie, |
| 1831 | + }, |
| 1832 | + } |
| 1833 | + ) |
| 1834 | + |
| 1835 | + expect(apiRes.status).toBe(200) |
| 1836 | + expect(await apiRes.json()).toEqual({ revalidated: true }) |
| 1837 | + |
| 1838 | + const postRevalidateRes = await fetchViaHTTP(next.url, '/preview') |
| 1839 | + expect(initialRes.status).toBe(200) |
| 1840 | + |
| 1841 | + const postRevalidate$ = cheerio.load(await postRevalidateRes.text()) |
| 1842 | + const postRevalidateProps = JSON.parse(postRevalidate$('#props').text()) |
| 1843 | + |
| 1844 | + expect(postRevalidateProps).toEqual({ |
| 1845 | + preview: false, |
| 1846 | + previewData: null, |
| 1847 | + }) |
| 1848 | + }) |
| 1849 | + |
1775 | 1850 | it('should handle revalidating HTML correctly', async () => { |
1776 | 1851 | const route = '/blog/post-2/comment-2' |
1777 | 1852 | const initialHtml = await renderViaHTTP(next.url, route) |
|
0 commit comments