@@ -48,65 +48,78 @@ test<FixtureTestContext>('Test that the simple next app is working', async (ctx)
48
48
expect ( load ( other . body ) ( 'h1' ) . text ( ) ) . toBe ( 'Other' )
49
49
} )
50
50
51
- test < FixtureTestContext > (
52
- 'should have two pages with fetch pre rendered and revalidated' ,
53
- async ( ctx ) => {
54
- await createFixture ( 'revalidate-fetch' , ctx )
55
- console . time ( 'runPlugin' )
56
- await runPlugin ( ctx )
57
- console . timeEnd ( 'runPlugin' )
58
- // check if the blob entries where successful set on the build plugin
59
- const blobEntries = await getBlobEntries ( ctx )
60
- expect ( blobEntries ) . toEqual ( [
61
- {
62
- key : 'cache/fetch-cache/460ed46cd9a194efa197be9f2571e51b729a039d1cff9834297f416dce5ada29' ,
63
- etag : expect . any ( String ) ,
64
- } ,
65
- {
66
- key : 'cache/fetch-cache/ad74683e49684ff4fe3d01ba1bef627bc0e38b61fa6bd8244145fbaca87f3c49' ,
67
- etag : expect . any ( String ) ,
68
- } ,
69
- { key : 'server/app/_not-found' , etag : expect . any ( String ) } ,
70
- { key : 'server/app/index' , etag : expect . any ( String ) } ,
71
- { key : 'server/app/posts/1' , etag : expect . any ( String ) } ,
72
- { key : 'server/app/posts/2' , etag : expect . any ( String ) } ,
73
- ] )
51
+ test < FixtureTestContext > ( 'should have a page prerendered, then wait for it to get stale and on demand revalidate it' , async ( ctx ) => {
52
+ await createFixture ( 'revalidate-fetch' , ctx )
53
+ console . time ( 'runPlugin' )
54
+ await runPlugin ( ctx )
55
+ console . timeEnd ( 'runPlugin' )
56
+ // check if the blob entries where successful set on the build plugin
57
+ const blobEntries = await getBlobEntries ( ctx )
58
+ expect ( blobEntries ) . toEqual ( [
59
+ {
60
+ key : 'cache/fetch-cache/460ed46cd9a194efa197be9f2571e51b729a039d1cff9834297f416dce5ada29' ,
61
+ etag : expect . any ( String ) ,
62
+ } ,
63
+ {
64
+ key : 'cache/fetch-cache/ad74683e49684ff4fe3d01ba1bef627bc0e38b61fa6bd8244145fbaca87f3c49' ,
65
+ etag : expect . any ( String ) ,
66
+ } ,
67
+ { key : 'server/app/_not-found' , etag : expect . any ( String ) } ,
68
+ { key : 'server/app/index' , etag : expect . any ( String ) } ,
69
+ { key : 'server/app/posts/1' , etag : expect . any ( String ) } ,
70
+ { key : 'server/app/posts/2' , etag : expect . any ( String ) } ,
71
+ ] )
72
+
73
+ // test the function call
74
+ const post1 = await invokeFunction ( ctx , { url : 'posts/1' } )
75
+ const post1Date = load ( post1 . body ) ( '[data-testid="date-now"]' ) . text ( )
76
+ expect ( post1 . statusCode ) . toBe ( 200 )
77
+ expect ( load ( post1 . body ) ( 'h1' ) . text ( ) ) . toBe ( 'Revalidate Fetch' )
78
+ expect ( post1 . headers ) . toEqual (
79
+ expect . objectContaining ( {
80
+ 'x-nextjs-cache' : 'HIT' ,
81
+ 'netlify-cdn-cache-control' : 's-maxage=3, stale-while-revalidate' ,
82
+ } ) ,
83
+ )
74
84
75
- // test the function call
76
- const post1 = await invokeFunction ( ctx , { url : 'posts/1' } )
77
- const post1Date = load ( post1 . body ) ( '[data-testid="date-now"]' ) . text ( )
78
- expect ( post1 . statusCode ) . toBe ( 200 )
79
- expect ( load ( post1 . body ) ( 'h1' ) . text ( ) ) . toBe ( 'Revalidate Fetch' )
80
- expect ( post1 . headers ) . toEqual (
81
- expect . objectContaining ( {
82
- 'x-nextjs-cache' : 'HIT' ,
83
- 'netlify-cdn-cache-control' : 's-maxage=3, stale-while-revalidate' ,
84
- } ) ,
85
- )
85
+ expect ( await ctx . blobStore . get ( 'server/app/posts/3' ) ) . toBeNull ( )
86
+ // this page is not pre-rendered and should result in a cache miss
87
+ const post3 = await invokeFunction ( ctx , { url : 'posts/3' } )
88
+ expect ( post3 . statusCode ) . toBe ( 200 )
89
+ expect ( load ( post3 . body ) ( 'h1' ) . text ( ) ) . toBe ( 'Revalidate Fetch' )
90
+ expect ( post3 . headers ) . toEqual (
91
+ expect . objectContaining ( {
92
+ 'x-nextjs-cache' : 'MISS' ,
93
+ } ) ,
94
+ )
86
95
87
- // this page is not pre-rendered and should result in a cache miss
88
- const post3 = await invokeFunction ( ctx , { url : 'posts/3' } )
89
- expect ( post3 . statusCode ) . toBe ( 200 )
90
- expect ( load ( post3 . body ) ( 'h1' ) . text ( ) ) . toBe ( 'Revalidate Fetch' )
91
- expect ( post3 . headers ) . toEqual (
92
- expect . objectContaining ( {
93
- 'x-nextjs-cache' : 'MISS' ,
94
- } ) ,
95
- )
96
+ // wait to have a stale page
97
+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 1_000 ) )
98
+ // after the dynamic call of `posts/3` it should be in cache, not this is after the timout as the cache set happens async
99
+ expect ( await ctx . blobStore . get ( 'server/app/posts/3' ) ) . not . toBeNull ( )
96
100
97
- // TODO: uncomment once stale is implemented via the cache tags manifest
98
- // wait 500ms to have a stale page
99
- // await new Promise<void>((resolve) => setTimeout(resolve, 500))
101
+ const stale = await invokeFunction ( ctx , { url : 'posts/1' } )
102
+ const staleDate = load ( stale . body ) ( '[data-testid="date-now"]' ) . text ( )
103
+ expect ( stale . statusCode ) . toBe ( 200 )
104
+ // it should have a new date rendered
105
+ expect ( staleDate , 'the date was cached and is matching the initial one' ) . not . toBe ( post1Date )
106
+ expect ( stale . headers ) . toEqual (
107
+ expect . objectContaining ( {
108
+ 'x-nextjs-cache' : 'MISS' ,
109
+ } ) ,
110
+ )
100
111
101
- // const stale = await invokeFunction(ctx, { url: 'posts/1' })
102
- // expect(stale.statusCode).toBe(200)
103
- // // it should have a new date rendered
104
- // expect(load(stale.body)('[data-testid="date-now"]').text()).not.toBe(post1Date)
105
- // expect(stale.headers).toEqual(
106
- // expect.objectContaining({
107
- // 'x-nextjs-cache': 'MISS',
108
- // }),
109
- // )
110
- } ,
111
- { retry : 3 } ,
112
- )
112
+ // it does not wait for the cache.set so we have to manually wait here until the blob storage got populated
113
+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 100 ) )
114
+
115
+ // now the page should be in cache again and we should get a cache hit
116
+ const cached = await invokeFunction ( ctx , { url : 'posts/1' } )
117
+ const cachedDate = load ( cached . body ) ( '[data-testid="date-now"]' ) . text ( )
118
+ expect ( cached . statusCode ) . toBe ( 200 )
119
+ expect ( staleDate , 'the date was not cached' ) . toBe ( cachedDate )
120
+ expect ( cached . headers ) . toEqual (
121
+ expect . objectContaining ( {
122
+ 'x-nextjs-cache' : 'HIT' ,
123
+ } ) ,
124
+ )
125
+ } )
0 commit comments