weixin mini νλ‘κ·Έλ¨μμ apollo-link-restλ₯Ό μ¬μ©νλ €κ³ ν λ μλμ κ°μ μ€λ₯κ° λ°μνλ λ¬Έμ κ° λ°μνμ΅λλ€.
Headers is not defined
ReferenceError: Headers is not defined
RestLink.tsμμ μμ€ μ½λλ₯Ό μ°Ύμμ λ ν€λλ₯Ό μ°ΎμΌλ €κ³ νμ§λ§ μ λμ μΌλ‘ weixin νκ²½μλ ν€λκ° μμΌλ―λ‘ λꡬλ μ§μ΄ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ ν΄νΉμ΄λ μ루μ μ μ 곡 ν μ μμ΅λκΉ? λμμ μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€. κ°μ¬ν©λλ€.
μλ νμΈμ @ Janice1114 , μ λ
μ’ μ’ ν€λλ₯Ό μ¬μ©ν μμλ κ²½μ° κ°μ Έ μ€κΈ°λ μ¬μ©ν μ μμ΅λλ€. λ°λΌμ λ λ€ μλ ν μμλ polyfillμ λ€μκ³Ό κ°μ΅λλ€. https://github.com/bitinn/node-fetch
μλ νμΈμ @fbartho , λΉ λ₯Έ λ΅μ₯μ κ°μ¬λ립λλ€. polyfillμ μ¬μ©νμ¬ ν€λ κ°μ Έ μ€κΈ°λ₯Ό νμ±ννλ €κ³ μλνμ§λ§ μ¬μ ν μ€λ₯κ° λ°μν©λλ€.
κ·Έλ¦¬κ³ weixinμμ μ°½ κ΄λ ¨ λ³μλ₯Ό μ§μνμ§ μλ fetchμ wx.requestλ₯Ό μ¬μ©νλ―λ‘ polyfillμ΄ μλνμ§ μμ κΉλ΄ κ±±μ λ©λλ€.
κ·Έλ¦¬κ³ apollo-link-restμ μμ€ μ½λμμ ν€λ 루νλ₯Ό 건λ λ°λ λ° λμμ΄λλ ν΄νΉμ΄λ ꡬνμ΄ μλμ§ μ μ μμ΅λκΉ? μλλ©΄ λ€λ₯Έ μλ리μ€μ λ§κ² λͺ κ°μ§ μ¬μ©μ μ§μ μ΅μ μ μ 곡 ν μ μμ΅λκΉ?
λνΌ ν¨μλ₯Ό ββμ 곡νκΈ° μν΄ ApolloLinkRestμ customFetch
λ§€κ° λ³μλ₯Ό μ¬μ©ν μ μμ΅λλ€.μ΄ λνΌ ν¨μλ Polyfillμμ μ°½ κΈ°λ° ν€λλ₯Ό κ°μ Έ μμ κΈ°μ‘΄ κ°μ²΄ ν΄μλ‘ λ³ν© ν μ μμ΅λλ€.
λ€μκ³Ό κ°μ κ² (λ΄ λΈλΌμ°μ μμ μμ±λμ§ μμ) :
function customFetch(url, options) {
const headers = options.headers.entries().reduce((accumulator, h) => { accumulator[h[0]] = h[1]; return accumulator;}, {})
return fetch(url, {β¦options, headers});
}
// Elsewhere: new RestLink({ customFetch, ...
μΉμ νκ² λ΅λ³ ν΄ μ£Όμ μ κ°μ¬ν©λλ€.μ΄ polyfillμ weixin κ°λ°μ μ¬μ©νλλ‘ λ Έλ ₯νκ² μ΅λλ€.
κ·Έλ¦¬κ³ RestLink.tsμμ μμ€ μ½λλ₯Ό μ½μΌλ©΄ weixinμμ μ¬μ©ν μμλ ν€λμ μ‘μΈμ€νλ €κ³ μλν©λλ€. global
λ³μλ₯Ό ν λΉ ν μ μμ΅λλ€.
λ°λΌμ RestLink μμ±μ νλ‘μΈμ€ μ€μ μ¬μ©μ μ§μ λλ μ¬μ μ λ μ€μ μ μ μ© ν μ μλμ§ κΆκΈν©λλ€.
λ©μ§ νμ λ€μ νλ² κ°μ¬λ립λλ€.
μλ
νμΈμ @fbartho ,
ν€λ λ¬Έμ μμ
μ½λ PRμ λ§μ€ν°μ λ³ν© ν μ μλμ§ κ²ν νκ³ λμ μ£Όμκ² μ΅λκΉ?
λμμ΄ λ μ μλ€λ©΄ λλ¨ν κ°μ¬νκ³ κ°μ¬ν©λλ€.
"next": "^9.0.3"
μλ κ°μ λ¬Έμ κ° μμ΅λλ€.
λ€μ λ²μ ^8.1.0
μμλμ΄ λ¬Έμ κ° λ°μν©λλ€. λ΄ κ·Έλν APIμ κ²½μ° isomorphic-unfetch
, λ²μ ^3.0.0
μ μ¬μ©νμ¬ λ€μκ³Ό κ°μ΄ ν΄λ¦¬ νν©λλ€.
import fetch from 'isomorphic-unfetch'
const graphLink = createHttpLink({ uri, fetch })
κ·Έλ¬λ λμΌν ν¨ν€μ§λ₯Ό μ¬μ©νμ¬ λλ¨Έμ§ λ§ν¬λ‘ polyfillμ μ¬μ©νλ©΄ μλνμ§ μμμ΅λλ€.
const restLink = new RestLink({
uri,
customFetch: fetch,
})
λν customFetch λ©μλλ₯Ό μ¬μ©νμ¬ node-fetch
μλνμ΅λλ€.
import fetch from 'node-fetch'
function customFetch(url, options) {
const headers = options.headers.entries().reduce((accumulator, h) => {
accumulator[h[0]] = h[1]
return accumulator
}, {})
return fetch(url, { ...options, headers })
}
const restLink = new RestLink({
uri: CONTENTFUL_CONTENT_DELIVERY_API,
customFetch,
})
λ κ²½μ° λͺ¨λ λ€μ κ°λ° λͺ¨λμμ μ€νν λ ReferenceError: Headers is not defined
λ°μμ΅λλ€.
μ¬κΈ°μμ μμ΄λμ΄λ₯Ό μ¬λνκ³ , μλ² νκ²½μμ ꡬννλ λ°©λ²μ λν μ§μΉ¨μ μ 곡 ν μ μλ€λ©΄ μ’μν κ²μ λλ€. κ°μ¬!
μμ λ§μ customFetch APIλ₯Ό μ¬μ©νλ©΄μ Headers APIλ₯Ό ν΄λ¦¬ ν ν μ μμ΅λλ€!
μν μ½λλ₯Ό μνμλ©΄ μλ €μ£ΌμΈμ.νμ§λ§ λ무 λ²κ±° λ‘μμμ΄ κ°λ₯νλ€κ³ νμ ν©λλ€. λλ μ§κΈ λ°λ‘ λ΄ μ»΄ν¨ν°μ μμ§ μμ΅λλ€.
@fbartho κΈ°νκ°μμ λ λͺ κ°μ§ μν μ½λκ° μ’μ΅λλ€! λλ κ·Έκ²μ μ° λ μ§ λ§ μ±κ³΅νμ§ λͺ»νμ΅λλ€. κ°μ¬!
import * as Polyfillheaders from 'fetch-headers'
import fetch from 'isomorphic-unfetch'
// hook in the Headers polyfill for your environment!
global.Headers = global.Headers || Polyfillheaders;
function customFetch(url, options) {
const headers = options.headers.entries().reduce((accumulator, h) => {
accumulator[h[0]] = h[1]
return accumulator
}, {})
return fetch(url, { ...options, headers })
}
const restLink = new RestLink({
uri: CONTENTFUL_CONTENT_DELIVERY_API,
customFetch,
})
μμ κ°μ κ²μ΄ @ 2wheelcoder νΈλ¦μ ν μ μμ΅λλ€.
κ°μ¬! μ΄ κΈ°νλ₯Ό μ‘μ ν μ¬κΈ°λ‘ λ€μλ³΄κ³ νκ² μ΅λλ€.
@fbartho μλ
νμΈμ! ios 9.3.5μμμ΄ λ¬Έμ κ° λ°μνμ΅λλ€. fetch-headers
polyfill κ°μ Έ μ€κΈ°λ₯Ό μλνμ§λ§ μλνμ§ μμ΅λλ€.
fetch-headers
μ ν€λ μΈμ€ν΄μ€μ forEach
λ©μλκ°μλ κ²μΌλ‘ λνλ¬μ΅λλ€ (μλ μ°Έμ‘°).
λ€λ₯Έ ν΄κ²°μ± μ΄ μμ΅λκΉ?
@fbartho μλ νμΈμ! ios 9.3.5μμμ΄ λ¬Έμ κ° λ°μνμ΅λλ€.
fetch-headers
polyfill κ°μ Έ μ€κΈ°λ₯Ό μλνμ§λ§ μλνμ§ μμ΅λλ€.
fetch-headers
μ ν€λ μΈμ€ν΄μ€μforEach
λ©μλκ°μλ κ²μΌλ‘ λνλ¬μ΅λλ€ (μλ μ°Έμ‘°).λ€λ₯Έ ν΄κ²°μ± μ΄ μμ΅λκΉ?
fetch polyfillμ μ¬μ©νμ¬μ΄ λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€.
μ± μΈ‘μμ ν΄λ¦¬ νλ‘μ΄ λ¬Έμ λ₯Ό ν΄κ²°ν μ μκ³ ν΄λ¦¬ νμμ΄ μ μ₯μμ μ’ μμ±μΌλ‘ λ§λ€κ³ μΆμ§ μκΈ° λλ¬Έμμ΄ λ¬Έμ λ₯Ό λ«κ² μ΅λλ€.
λλ μ΄κ²μ polyfillλ‘ ν΄κ²°ν μ μμμ΅λλ€. λλ isomorphic-fetch, isomorphic-unfetch, fetch-headers λ±μ μλνκ³ νμ current.forEach
not being defined λλ TypeError: Right-hand side of 'instanceof' is not an object
λν λ¬Έμ λ‘ λλ©λλ€. λμμ΄ νμνμΈμ?
@VinSpee μ΄ κ°μ Έ μ€κΈ° polyfillμ μ¬μ©ν΄ λ³Έ μ μ΄ μμ΅λκΉ?
@lintuming μ,νμ§λ§ μλ²μμ μλνμ§ μμ΅λλ€.
@VinSpee μλ§λ μ΄κ²μ μ¬μ©ν μ μμ΅λλ€. forEach
λ°©λ²μ΄μλ κ² κ°μ΅λλ€.
μ΄ μΌμ λ°λλλ€.
@lintuming λμμ μ£Όμ μ κ°μ¬ν©λλ€. κ·Έκ²μΌλ‘ λλ λ€μμ μ»μ΅λλ€.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
μ½ν λ¬Έμ μ κ·Έλ¬Όμ²λΌ 보μ λλ€ π€£
@VinSpee μ£μ‘ν©λλ€, μ μ΄λ° μΌμ΄ λ°μνλμ§ λͺ¨λ₯΄κ² μ§λ§ μ΄κ²μ΄ λμμ΄ λ μ μλ€κ³ μκ°ν©λλ€.
https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client
λλ₯Ό μν΄ μΌν κ²°κ³Όλ λ€μκ³Ό κ°μ΅λλ€.
import clientFetch from 'unfetch';
import serverFetch, { Headers as ServerHeaders } from 'node-fetch';
const client = typeof document !== 'undefined';
global.Headers = client ? global.Headers : ServerHeaders;
const customFetch = client ? clientFetch : serverFetch;
const acmeLink = new JsonApiLink({
β¦
credentials: 'same-origin',
customFetch,
});
@VinSpee λλ λμΌν λ¬Έμ μ μ§λ©΄νκ³ μμΌλ©° μ루μ
μΌλ‘μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ €κ³ νμ§λ§ μ±κ³΅νμ§ λͺ»νμ΅λλ€.
μμ
μ½λλ₯Ό 곡μ ν΄ μ£Όμκ² μ΅λκΉ?
@VinSpee μκ² κ°μ¬λ립λλ€.
λ§μΆ€ κ°μ Έ μ€κΈ°λ₯Ό μ¬μ©νμ¬μ΄ λ¬Έμ λ₯Ό ν΄κ²°ν μμμμ΅λλ€.
import fetch from "isomorphic-fetch"
const restLink = new RestLink({
uri: "https://...",
customFetch: fetch,
headers: {
"Content-Type": "application/json",
},
})
@VinSpee μ μ루μ μ Gatsby JS μ ν리μΌμ΄μ μμλ μ μκ² ν¨κ³Όμ μ΄μμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
λλ₯Ό μν΄ μΌν κ²°κ³Όλ λ€μκ³Ό κ°μ΅λλ€.