ํญ์ ๊ฒฝ๋ก ๋งค๊ฐ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฉ๋ฌธํ๋์ง ํ์ธํ๋ ค๋ /trial
ํ์ด์ง๊ฐ ์์ต๋๋ค. ๋ด ์์ฑ์์์ this.props.location.search
๋ฅผ ํ์ธํ๊ณ ๊ฑฐ์ง (๋น์ด ์์)์ด๋ฉด /trial/?product=foobar
๋ก ์ด๋ํ๊ณ ์ถ์ต๋๋ค.
๋ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import React from 'react'
import { navigate } from 'gatsby'
class Foobar extends React.Component {
constructor(props) {
super(props)
this.redirectIfNecessary()
}
redirectIfNecessary() {
// Make prospect the default product
const { location } = this.props
const { search } = location
if (!search) {
navigate('/trial/?product=foobar', {
state: {
product: 'foobar',
},
})
}
}
render() {
return <div>Hello, world</div>
}
}
์ด๊ฒ์ ๊ฐ๋ฐ ์๊ฐ ๋์ ์ ์๋ํ์ง๋ง gatsby build
์คํํ๋ฉด
error Building static HTML for pages failed
See our docs page on debugging HTML builds for help https://goo.gl/yL9lND
186 |
187 | var navigate = function navigate(to, options) {
> 188 | window.___navigate(withPrefix(to), options);
| ^
189 | };
190 |
191 | exports.navigate = navigate;
WebpackError: ReferenceError: window is not defined
gatsby info --clipboard
System:
OS: macOS 10.14.1
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Shell: 5.3 - /bin/zsh
Binaries:
Node: 11.3.0 - /usr/local/bin/node
Yarn: 1.12.3 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Browsers:
Chrome: 70.0.3538.110
Firefox: 63.0.3
Safari: 12.0.1
npmPackages:
gatsby: ^2.0.64 => 2.0.64
gatsby-image: ^2.0.20 => 2.0.22
gatsby-plugin-manifest: ^2.0.10 => 2.0.11
gatsby-plugin-offline: ^2.0.19 => 2.0.19
gatsby-plugin-react-helmet: ^3.0.4 => 3.0.4
gatsby-plugin-react-svg: ^2.0.0 => 2.0.0
gatsby-plugin-sass: ^2.0.7 => 2.0.7
gatsby-plugin-sharp: ^2.0.15 => 2.0.15
gatsby-source-filesystem: ^2.0.11 => 2.0.11
gatsby-transformer-sharp: ^2.1.8 => 2.1.9
npmGlobalPackages:
gatsby-cli: 2.4.6
์๋ ํ์ธ์ @kevinmichaelchen
window
์ (๋) ๋น๋ ์๊ฐ์ด ์๋ ๋ธ๋ผ์ฐ์ ์์๋ง ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์ Webpack์์ด๋ฅผ ๋น๋ํ์ง ๋ชปํ์ต๋๋ค.
๋์ redirectIfNecessary
ํธ์ถ์ constructor
์์ componentDidMount
๋ก ์ด๋ํ์ฌ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค. ์ด๋ ๊ฒํ๋ฉด ๋น๋ ์๊ฐ์ด ์๋ ํด๋ผ์ด์ธํธ ์ธก์์๋ง ์คํ๋ฉ๋๋ค.
๋๋ฌ ๋ผ์ฐํฐ ์
์ด ๋ฌธ์ ๋ Gatsby์๋ง ๊ตญํ๋ ๊ฒ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ง๊ธ์์ด ๋ฌธ์ ๋ฅผ ๋ซ๊ฒ ์ต๋๋ค. ๋ค๋ฅธ ๋ฌธ์ ๊ฐ ์์ผ๋ฉด ์ธ์ ๋ ์ง ๋ค์ ์ด์ด๋ณด์ธ์.
์ฌ์ฉ์ ๋ธ๋ผ์ฐ์ ๋ชจ๋๋ ์ ์ฅ์๋ ์ด์ ๋ํ ๋น ๋ฅธ ์์ ์ ๋๋ค!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์๋ ํ์ธ์ @kevinmichaelchen
window
์ (๋) ๋น๋ ์๊ฐ์ด ์๋ ๋ธ๋ผ์ฐ์ ์์๋ง ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์ Webpack์์ด๋ฅผ ๋น๋ํ์ง ๋ชปํ์ต๋๋ค.๋์
redirectIfNecessary
ํธ์ถ์constructor
์์componentDidMount
๋ก ์ด๋ํ์ฌ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค. ์ด๋ ๊ฒํ๋ฉด ๋น๋ ์๊ฐ์ด ์๋ ํด๋ผ์ด์ธํธ ์ธก์์๋ง ์คํ๋ฉ๋๋ค.๋๋ฌ ๋ผ์ฐํฐ ์
์ด ๋ฌธ์ ๋ Gatsby์๋ง ๊ตญํ๋ ๊ฒ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ง๊ธ์์ด ๋ฌธ์ ๋ฅผ ๋ซ๊ฒ ์ต๋๋ค. ๋ค๋ฅธ ๋ฌธ์ ๊ฐ ์์ผ๋ฉด ์ธ์ ๋ ์ง ๋ค์ ์ด์ด๋ณด์ธ์.