1
0
mirror of synced 2025-12-23 03:44:00 -05:00
Files
docs/tests/helpers/script-data.js
Peter Bengtsson 18504871b9 cache full rendering (#25424)
* cache full rendering

* still not working with gzip

* progress progress progress

* smaller

* hacky progress

* small fixes

* wip

* lock file

* wip

* wip

* package-lock updates

* wip

* search DOM in lowercase

* simplify

* with instrument

* improve test coverage

* mutateCheeriobodyByRequest

* fix

* remove renderContentCacheByContex

* disable render caching in sync-search

* diables things in github/github link checker

* gzip lru

* tidying up

* updated

* correct tests

* fix: move userLanguage to LanguagesContext

* Revert "fix: move userLanguage to LanguagesContext"

This reverts commit d7c05d958c71eaad496eb46764eb845d80b866ca.

* contexts ftw

* fixed rendering tests

* oops for got new file

* nits addressed

Co-authored-by: Mike Surowiec <mikesurowiec@users.noreply.github.com>
2022-05-23 12:12:09 +00:00

30 lines
1.1 KiB
JavaScript

const NEXT_DATA_QUERY = 'script#__NEXT_DATA__'
const PRIMER_DATA_QUERY = 'script#__PRIMER_DATA__'
function getScriptData($, key) {
const data = $(key)
if (!data.length === 1) {
throw new Error(`Not exactly 1 element match for '${key}'. Found ${data.length}`)
}
return JSON.parse(data.get()[0].children[0].data)
}
export const getNextData = ($) => getScriptData($, NEXT_DATA_QUERY)
export const getPrimerData = ($) => getScriptData($, PRIMER_DATA_QUERY)
export const getUserLanguage = ($) => {
// Because the page might come from the middleware rendering cache,
// the DOM won't get updated until the first client-side React render.
// But we can assert the data that would be used for that first render.
const { props } = getNextData($)
return props.languagesContext.userLanguage
}
export const getIsDotComAuthenticated = ($) => {
// Because the page might come from the middleware rendering cache,
// the DOM won't get updated until the first client-side React render.
// But we can assert the data that would be used for that first render.
const { props } = getNextData($)
return props.dotComAuthenticatedContext.isDotComAuthenticated
}