This file contains hidden or 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
export default function NewPost() { | |
const navigate = useNavigate(); | |
const invalidateCache = useCacheInvalidate(); | |
// ... | |
/** @type React.FormEventHandler */ | |
const handleSubmit = async (e) => { | |
e.preventDefault(); | |
await createPost({title, content}); | |
invalidateCache('/posts'); | |
navigate('/posts'); |
This file contains hidden or 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
router.post( | |
'/', | |
handleErrors(async (req, res) => { | |
const post = await prisma.post.create({ | |
data: { | |
title: req.body.title, | |
content: req.body.content, | |
}, | |
}); |
This file contains hidden or 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
export function useCacheInvalidate() { | |
const cache = unstable_getCacheForType(createResponseCache); | |
return (key) => { | |
cache.delete(key); | |
}; | |
} |
This file contains hidden or 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 | |
export function useServerResponse(location) { | |
const key = JSON.stringify(location); | |
const cache = unstable_getCacheForType(createResponseCache); | |
let response = cache.get(key); | |
if (response) { | |
return response; | |
} | |
response = createFromFetch( | |
fetch('/react?location=' + encodeURIComponent(key)) |
This file contains hidden or 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
function sendResponse(req, res, component, props = null) { | |
props = props ?? {}; | |
renderReactTree(res, component, props); | |
} | |
router.get('/', function(req, res) { | |
sendResponse(req, res, Home); | |
}); | |
router.get('/posts', (req, res) => { |
This file contains hidden or 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 demo. | |
async function renderReactTree(res, props) { | |
await waitForWebpack(); | |
const manifest = readFileSync( | |
path.resolve(__dirname, '../build/react-client-manifest.json'), | |
'utf8' | |
); | |
const moduleMap = JSON.parse(manifest); | |
pipeToNodeWritable(React.createElement(ReactApp, props), res, moduleMap); | |
} |
This file contains hidden or 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
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | |
const makeStyles = (theme: Theme) => | |
StyleSheet.create({ | |
url: { | |
color: theme.link, | |
textDecorationLine: 'underline', | |
}, | |
}); | |
This file contains hidden or 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
const contentMutation = graphql` | |
mutation MyComponentContentMutation( | |
$postId: ID! | |
) { | |
deletePostContent(postId: $postId) { | |
id | |
content | |
} | |
} | |
`; |
This file contains hidden or 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
type Mutation { | |
deletePostContent(postId: ID!): Boolean | |
} |
This file contains hidden or 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
const titleMutation = graphql` | |
mutation MyComponentTitleMutation( | |
$postId: ID! | |
$title: String! | |
) { | |
changePostTitle(postId: $postId, title: $title) { | |
id | |
title | |
} | |
} |