-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
431 additions
and
209 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
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,56 @@ | ||
import { isValidRequest } from '@sanity/webhook' | ||
import { sanityClient } from '../../lib/sanity.server' | ||
|
||
const AUTHOR_UPDATED_QUERY = ` | ||
*[_type == "author" && _id == $id] { | ||
"slug": *[_type == "post" && references(^._id)].slug.current | ||
}["slug"][]` | ||
const POST_UPDATED_QUERY = `*[_type == "post" && _id == $id].slug.current` | ||
|
||
const getQueryForType = (type) => { | ||
switch (type) { | ||
case 'author': | ||
return AUTHOR_UPDATED_QUERY | ||
case 'post': | ||
return POST_UPDATED_QUERY | ||
default: | ||
throw new TypeError(`Unknown type: ${type}`) | ||
} | ||
} | ||
|
||
const log = (msg, error) => | ||
console[error ? 'error' : 'log'](`[revalidate] ${msg}`) | ||
|
||
export default async function revalidate(req, res) { | ||
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 | ||
if (typeof id !== 'string' || !id) { | ||
const invalidId = 'Invalid _id' | ||
log(invalidId, true) | ||
return res.status(400).json({ message: invalidId }) | ||
} | ||
|
||
log(`Querying post slug for _id '${id}', type '${_type}' ..`) | ||
const slug = await sanityClient.fetch(getQueryForType(_type), { id }) | ||
const slugs = (Array.isArray(slug) ? slug : [slug]).map( | ||
(_slug) => `/posts/${_slug}` | ||
) | ||
const staleRoutes = ['/', ...slugs] | ||
|
||
try { | ||
await Promise.all( | ||
staleRoutes.map((route) => res.unstable_revalidate(route)) | ||
) | ||
const updatedRoutes = `Updated routes: ${staleRoutes.join(', ')}` | ||
log(updatedRoutes) | ||
return res.status(200).json({ message: updatedRoutes }) | ||
} catch (err) { | ||
log(err.message, true) | ||
return res.status(500).json({ message: err.message }) | ||
} | ||
} |
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,30 @@ | ||
import Document from 'next/document' | ||
import { ServerStyleSheet } from 'styled-components' | ||
|
||
export default class MyDocument extends Document { | ||
static async getInitialProps(ctx) { | ||
const sheet = new ServerStyleSheet() | ||
const originalRenderPage = ctx.renderPage | ||
|
||
try { | ||
ctx.renderPage = () => | ||
originalRenderPage({ | ||
enhanceApp: (App) => (props) => | ||
sheet.collectStyles(<App {...props} />), | ||
}) | ||
|
||
const initialProps = await Document.getInitialProps(ctx) | ||
return { | ||
...initialProps, | ||
styles: ( | ||
<> | ||
{initialProps.styles} | ||
{sheet.getStyleElement()} | ||
</> | ||
), | ||
} | ||
} finally { | ||
sheet.seal() | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,5 +16,5 @@ | |
"registry": "https://registry.npmjs.org/" | ||
} | ||
}, | ||
"version": "12.1.1-canary.8" | ||
"version": "12.1.1-canary.9" | ||
} |
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
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
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
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
Oops, something went wrong.