Skip to content

Commit 31d0e8a

Browse files
committed
Merge branch 'repro-no-ssr'
2 parents d6cabd7 + 59f6884 commit 31d0e8a

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

gatsby-config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ const shareImageConfig = {
228228
}
229229

230230
module.exports = {
231+
flags: {
232+
PRESERVE_WEBPACK_CACHE: true, // https://github.com/gatsbyjs/gatsby/discussions/28331
233+
FAST_DEV: true,
234+
DEV_SSR: true,
235+
PRESERVE_FILE_DOWNLOAD_CACHE: true,
236+
},
231237
plugins: [
232238
{
233239
resolve: 'gatsby-theme-guide',

gatsby-ssr.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react'
2+
import { URL } from 'url'
3+
4+
function getLinkProps({ crossOrigin, pathname }) {
5+
switch (typeof crossOrigin) {
6+
case `string`:
7+
return { crossOrigin }
8+
case `function`:
9+
return getLinkProps({ crossOrigin: crossOrigin(pathname), pathname })
10+
default:
11+
return { crossOrigin: `anonymous` }
12+
}
13+
}
14+
15+
export const onRenderBody = (
16+
{ setHeadComponents, pathname = `/` },
17+
{ crossOrigin = `anonymous` } = {}
18+
) => {
19+
const props = getLinkProps({ crossOrigin, pathname })
20+
21+
const assets = [
22+
'https://fonts.gstatic.com/s/sourcecodepro/v11/HI_SiYsKILxRpg3hIP6sJ7fM7PqlPevT.ttf',
23+
'https://fonts.gstatic.com/s/sourcesanspro/v13/6xK3dSBYKcSV-LCoeQqfX1RYOo3qOK7g.ttf',
24+
'https://fonts.gstatic.com/s/sourcesanspro/v13/6xKydSBYKcSV-LCoeQqfX1RYOo3i54rwlxdr.ttf',
25+
]
26+
console.log('ON RENDER BODY')
27+
28+
setHeadComponents(
29+
assets.map((href) => {
30+
let assetProps
31+
32+
// External urls should get the props from the plugin configuration.
33+
// Local urls will be forced with `crossOrigin: "anonymous"`
34+
try {
35+
// check if URL is external, if not this constructor throws.
36+
new URL(href)
37+
assetProps = props
38+
} catch (e) {
39+
assetProps = { crossOrigin: `anonymous` }
40+
}
41+
42+
return (
43+
<link key={href} as="font" href={href} rel="preload" {...assetProps} />
44+
)
45+
})
46+
)
47+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"revalidate": "^1.2.0",
6767
"scroll-into-view-if-needed": "^2.2.26",
6868
"subscriptions-transport-ws": "^0.9.18",
69+
"url": "^0.11.0",
6970
"use-query-params": "^1.2.0"
7071
},
7172
"devDependencies": {

src/components/landing/Welcome.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Typography } from '@material-ui/core'
44
import { Link } from 'gatsby'
55
import queryString from 'query-string'
66
import { gql, useMutation } from '@apollo/client'
7+
import { pick } from 'lodash'
78

89
import './Welcome.css'
910
import { getPackage } from '../../lib/packages'
@@ -50,19 +51,21 @@ export default function Welcome({ location }) {
5051

5152
const [associateSignupToken, { error }] = useMutation(ASSOCIATE_SIGNUP_TOKEN)
5253

54+
const userDep = pick(user, ['id', 'hasPurchased'])
55+
5356
const query = queryString.parse(location.search)
5457
const inviteCode = query['invite-code']
5558
useEffect(() => {
56-
if (user && !user.hasPurchased && inviteCode) {
59+
if (userDep.id && !userDep.hasPurchased && inviteCode) {
5760
associateSignupToken({ variables: { token: inviteCode } })
5861
}
59-
}, [user?.id, inviteCode, associateSignupToken])
62+
}, [userDep, inviteCode, associateSignupToken])
6063

6164
useEffect(() => {
62-
if (user && !user.hasPurchased && !inviteCode) {
65+
if (userDep.id && !userDep.hasPurchased && !inviteCode) {
6366
pollAssociateSession()
6467
}
65-
}, [user?.id, inviteCode])
68+
}, [userDep, inviteCode])
6669

6770
useEffect(() => {
6871
if (!inviteCode) {

src/gatsby-theme-guide/components/paywall.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const FULL_PATHS = ['/service-integrations/', '/preventing-dos-attacks/']
2626

2727
const Hide = styled.div`
2828
height: ${(props) => (props.hide ? '1px' : 'auto')};
29-
overflow-y: hidden;
29+
overflow-y: ${(props) => (props.hide ? 'hidden' : 'auto')};
3030
`
3131

3232
const Overlay = styled.div`

text/preventing-dos-attacks/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ First, guarding against expensive requests—requests that take up significant r
1515
- Limit depth: One way to make a query expensive is to make it really deep—continuing to select connection fields (like `query { posts { comments { users { posts { comments { ...etc. }}}}}}`). We can use the [`graphql-depth-limit`](https://github.com/stems/graphql-depth-limit) library for this.
1616
- Limit complexity: This is a more advanced technique than just limiting depth and involves assigning a complexity cost value to each field and limiting the total cost of a query. We can implement this using [`graphql-validation-complexity`](https://github.com/4Catalyzer/graphql-validation-complexity), or, if we want more flexibility, [`graphql-cost-analysis`](https://github.com/pa-bru/graphql-cost-analysis), which allows us to multiply costs by arguments or parent multipliers.
1717

18-
We can guard against a large number of requests by rate limiting. GitHub uses a combination of [rate limiting and cost analysis](https://developer.github.com/v4/guides/resource-limitations/#rate-limit) for its public API—we can’t make queries with a total cost of more than 5,000 points per hour. There’s not yet an open-source library that does this. (If you write one, let us know so that we can link to it! And you may want to use a [leaky bucket algorithm](https://en.wikipedia.org/wiki/Leaky_bucket) like [Shopify does](https://shopify.dev/concepts/about-apis/rate-limits) instead of a fixed window.) The [`graphql-rate-limit-directive`](https://github.com/ravangen/graphql-rate-limit) library provides a directive that allows us to limit the number of times a particular field or object is selected within a certain time window.
18+
We can guard against a large number of requests by rate limiting. GitHub uses a combination of [rate limiting and cost analysis](https://developer.github.com/v4/guides/resource-limitations/#rate-limit) for its public API—we can’t make queries with a total cost of more than 5,000 points per hour. There’s not yet an open-source library that does this. (If you write one, let us know so that we can link to it! And you may want to use a [leaky bucket algorithm](https://en.wikipedia.org/wiki/Leaky_bucket) like [Shopify does](https://shopify.dev/concepts/about-apis/rate-limits) with its [cost analysis rate limiting](https://shopify.engineering/rate-limiting-graphql-apis-calculating-query-complexity) instead of a fixed window.) The [`graphql-rate-limit-directive`](https://github.com/ravangen/graphql-rate-limit) library provides a directive that allows us to limit the number of times a particular field or object is selected within a certain time window.
1919

2020
In addition to blocking requests that are too complex or too frequent, we can reduce the amount of resources each request takes. For instance, instead of doing all the work needed during the request, in some cases we can send a response and then queue a job to be executed by a different server, clearing more room for our API server to handle more requests. Another example is caching—we can reduce the load on our database by using a cache, which we talk about in [Chapter 11: Server > Performance > Caching](../server/extended-topics/performance.md#caching).
2121

0 commit comments

Comments
 (0)