-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from sanity-io/next-ssg
Statically Generated Sites & webhook-based on-demand page revalidation
- Loading branch information
Showing
4 changed files
with
191 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import urlJoin from 'proper-url-join'; | ||
import { isValidRequest } from '@sanity/webhook'; | ||
import client from '../../lib/sanity.server'; | ||
|
||
const SECTION_UPDATED_QUERY = ` | ||
*[_type == "page" && references($id)] { | ||
"slug": *[_type == "route" && references(^._id)].slug.current | ||
}["slug"][]`; | ||
const PAGE_UPDATED_QUERY = `*[_type == "route" && references($id)].slug.current`; | ||
const ROUTE_UPDATED_QUERY = `*[_type == "route" && _id == $id].slug.current`; | ||
|
||
const getQueryForType = (type: string) => { | ||
switch (type) { | ||
case 'route': | ||
return ROUTE_UPDATED_QUERY; | ||
case 'page': | ||
return PAGE_UPDATED_QUERY; | ||
default: | ||
return SECTION_UPDATED_QUERY; | ||
} | ||
}; | ||
|
||
const log = (msg: string, error?: boolean) => | ||
console[error ? 'error' : 'log'](`[revalidate] ${msg}`); | ||
|
||
export default async function revalidate( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
if (!isValidRequest(req, process.env.SANITY_STUDIO_REVALIDATE_SECRET)) { | ||
const invalidRequest = 'Invalid request'; | ||
log(invalidRequest, true); | ||
return res.status(401).json({ message: invalidRequest }); | ||
} | ||
|
||
const { _id: id, _type } = req.body as { _id: string; _type: string }; | ||
if (typeof id !== 'string' || !id) { | ||
const invalidId = 'Invalid _id'; | ||
log(invalidId, true); | ||
return res.status(400).json({ message: invalidId }); | ||
} | ||
|
||
log(`Querying for _id '${id}', type '${_type}' ..`); | ||
const relatedRoutes = await client.fetch(getQueryForType(_type), { id }); | ||
if (!Array.isArray(relatedRoutes) || !relatedRoutes.length) { | ||
const noUpdatedObjects = 'No pages reference updated object'; | ||
log(noUpdatedObjects, true); | ||
return res.status(200).json({ message: noUpdatedObjects }); | ||
} | ||
|
||
try { | ||
await Promise.all( | ||
relatedRoutes.map((route) => res.unstable_revalidate(urlJoin(route))) | ||
); | ||
const updatedRoutes = `Updated routes: ${relatedRoutes.join(', ')}`; | ||
log(updatedRoutes); | ||
return res.status(200).json({ message: updatedRoutes }); | ||
} catch (err) { | ||
log(err.message, true); | ||
return res.status(500).json({ message: err.message }); | ||
} | ||
} |
Oops, something went wrong.
3b1d979
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
structured-content-2022-web – ./web
structured-content-2022-web-git-main.sanity.build
structured-content-2022-web.sanity.build
structuredcontent.live
www.structuredcontent.live
3b1d979
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
structured-content-2022-studio – ./studio
admin.structuredcontent.live
structured-content-2022-studio-git-main.sanity.build
structured-content-2022-studio.sanity.build