-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
6944cc0
commit 67d98dd
Showing
7 changed files
with
90 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
*/10 * * * * curl -i -X POST "https://${DRAFT_MODE_HOSTNAME}/api/memoized-fns-invalidate?token=${SECRET_API_TOKEN}" | ||
0 */6 * * * [ "$DEPLOYMENT_DESTINATION" = "production" ] && curl -i -X PUT -H "Authorization: Bearer ${DATOCMS_API_TOKEN}" -H "Accept: application/json" -H "X-Api-Version: 3" 'https://site-api.datocms.com/build-triggers/34759/reindex' | ||
0 0 * * * [ "$DEPLOYMENT_DESTINATION" = "production" ] && curl -i -X PUT -H "Authorization: Bearer ${DATOCMS_API_TOKEN}" -H "Accept: application/json" -H "X-Api-Version: 3" 'https://site-api.datocms.com/build-triggers/34759/reindex' |
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,30 @@ | ||
import { DATOCMS_API_TOKEN } from 'astro:env/server'; | ||
import { dataSource } from '~/lib/dataSource'; | ||
import { TagFragment } from '~/lib/datocms/commonFragments'; | ||
import { graphql } from '~/lib/datocms/graphql'; | ||
import { rawExecuteQueryWithAutoPagination } from '~/lib/datocms/rawExecuteQueryWithAutoPagination'; | ||
|
||
const query = graphql( | ||
/* GraphQL */ ` | ||
query RootQuery { | ||
_site { | ||
faviconMetaTags { | ||
...TagFragment | ||
} | ||
} | ||
} | ||
`, | ||
[TagFragment], | ||
); | ||
|
||
export const [fetchFavicon, maybeInvalidateFavicon] = dataSource('favicon', async () => { | ||
const [result, datocmsGraphqlResponse] = await rawExecuteQueryWithAutoPagination(query, { | ||
returnCacheTags: true, | ||
excludeInvalid: true, | ||
token: DATOCMS_API_TOKEN, | ||
}); | ||
|
||
const cacheTags = datocmsGraphqlResponse.headers.get('x-cache-tags')!.split(' '); | ||
|
||
return [result, cacheTags] as const; | ||
}); |
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,42 @@ | ||
import type { AstroGlobal } from 'astro'; | ||
import { uniq } from 'lodash-es'; | ||
import { isDraftModeEnabled } from './draftMode'; | ||
|
||
export type AstroOrRequestResponseHeaders = | ||
| AstroGlobal | ||
| { | ||
request: Request; | ||
responseHeaders: Headers; | ||
}; | ||
|
||
export function augmentResponseHeadersWithSurrogateKeys( | ||
newSurrogateKeys: string[], | ||
astroOrRequestResponseHeaders: AstroOrRequestResponseHeaders, | ||
) { | ||
const draftModeEnabled = isDraftModeEnabled( | ||
'request' in astroOrRequestResponseHeaders | ||
? astroOrRequestResponseHeaders.request | ||
: astroOrRequestResponseHeaders, | ||
); | ||
|
||
const responseHeaders = | ||
'responseHeaders' in astroOrRequestResponseHeaders | ||
? astroOrRequestResponseHeaders.responseHeaders | ||
: astroOrRequestResponseHeaders.response.headers; | ||
|
||
const surrogateKeyHeaderName = draftModeEnabled ? 'debug-surrogate-key' : 'surrogate-key'; | ||
const existingSurrogateKeys = responseHeaders.get(surrogateKeyHeaderName)?.split(' ') ?? []; | ||
const mergedCacheTags = uniq([...existingSurrogateKeys, ...newSurrogateKeys]).join(' '); | ||
|
||
responseHeaders.set(surrogateKeyHeaderName, mergedCacheTags); | ||
responseHeaders.set('datocms-cache-tags', mergedCacheTags); | ||
|
||
if (draftModeEnabled) { | ||
responseHeaders.set('cache-control', 'private'); | ||
} else { | ||
responseHeaders.set( | ||
'surrogate-control', | ||
'max-age=31536000, stale-while-revalidate=60, stale-if-error=86400', | ||
); | ||
} | ||
} |
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