-
Notifications
You must be signed in to change notification settings - Fork 88
feat: refresh hooks api implementation #1950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
55d6022
c3d5607
3030689
699ad3a
8033d60
3c805d4
796e458
bf3f8d9
2a2da7e
cae0a91
b04cb8e
de649e9
98407ea
f691362
443beca
e043e9d
95f7c94
238596e
cf0fc72
0f7d1db
8cc7abe
fef98a1
bf97d73
fef1c59
99ed48d
3ec181f
6b4a2ce
021f825
a40925f
6aa81df
8f60f43
9fc4123
0e61a26
a8ea2bd
36acbdb
d53a11d
4e8daa5
8bcdb1c
ce62c92
3da9e46
228ed12
f85d2f5
a197783
5da076d
d029976
6ef27a5
1445c26
fd32f38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default async function handler(req, res) { | ||
try { | ||
const path = '/getStaticProps/with-revalidate/' | ||
await res.revalidate(path) | ||
console.log('Revalidated:', path) | ||
return res.json({ revalidated: true }) | ||
} catch (err) { | ||
return res.status(500).send('Error revalidating:', err) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,10 @@ export const generateFunctions = async ( | |
await ensureDir(join(functionsDir, functionName)) | ||
await writeFile(join(functionsDir, functionName, `${functionName}.js`), apiHandlerSource) | ||
await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js')) | ||
await copyFile( | ||
join(__dirname, '..', '..', 'lib', 'templates', 'server.js'), | ||
join(functionsDir, functionName, 'server.js'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get some comments on why we're copying this file? (This whole functions.ts file could use those tbh hah, but I won't put that on you) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, i totally agree! i don't know enough about some of the functions in there to comment it all, but i've added comments to |
||
) | ||
await copyFile( | ||
join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'), | ||
join(functionsDir, functionName, 'handlerUtils.js'), | ||
|
@@ -67,6 +71,10 @@ export const generateFunctions = async ( | |
await ensureDir(join(functionsDir, functionName)) | ||
await writeFile(join(functionsDir, functionName, `${functionName}.js`), handlerSource) | ||
await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js')) | ||
await copyFile( | ||
join(__dirname, '..', '..', 'lib', 'templates', 'server.js'), | ||
join(functionsDir, functionName, 'server.js'), | ||
) | ||
await copyFile( | ||
join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'), | ||
join(functionsDir, functionName, 'handlerUtils.js'), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import https from 'https' | ||
|
||
import { NodeRequestHandler, Options } from 'next/dist/server/next-server' | ||
|
||
import { getNextServer, NextServerType } from './handlerUtils' | ||
|
||
const NextServer: NextServerType = getNextServer() | ||
|
||
interface NetlifyNextServerOptions extends Options { | ||
netlifyRevalidateToken: string | ||
} | ||
|
||
class NetlifyNextServer extends NextServer { | ||
nickytonline marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private netlifyRevalidateToken?: string | ||
|
||
public constructor(options: NetlifyNextServerOptions) { | ||
super(options) | ||
this.netlifyRevalidateToken = options.netlifyRevalidateToken | ||
} | ||
|
||
public getRequestHandler(): NodeRequestHandler { | ||
const handler = super.getRequestHandler() | ||
return async (req, res, parsedUrl) => { | ||
if (req.headers['x-prerender-revalidate']) { | ||
if (this.netlifyRevalidateToken) { | ||
await this.netlifyRevalidate(req.url) | ||
orinokai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
throw new Error(`Missing revalidate token`) | ||
} | ||
} | ||
return handler(req, res, parsedUrl) | ||
} | ||
} | ||
|
||
private netlifyRevalidate(url: string) { | ||
const domain = this.hostname | ||
const siteId = process.env.SITE_ID | ||
|
||
return new Promise((resolve, reject) => { | ||
const body = JSON.stringify({ paths: [url], domain }) | ||
|
||
const req = https | ||
.request( | ||
{ | ||
hostname: 'api.netlify.com', | ||
port: 443, | ||
path: `/api/v1/sites/${siteId}/refresh_on_demand_builders`, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Content-Length': body.length, | ||
Authorization: `Bearer ${this.netlifyRevalidateToken}`, | ||
}, | ||
}, | ||
(res) => { | ||
let data = '' | ||
res.on('data', (chunk) => { | ||
data += chunk | ||
}) | ||
res.on('end', () => { | ||
resolve(JSON.parse(data)) | ||
}) | ||
}, | ||
) | ||
.on('error', reject) | ||
|
||
req.write(body) | ||
req.end() | ||
}) | ||
} | ||
} | ||
|
||
export { NetlifyNextServer } |
Uh oh!
There was an error while loading. Please reload this page.