fetch

Compatibility between cypress and react-apollo

人走茶凉 提交于 2021-02-10 14:22:59
问题 I am using react-apollo to make GraphQL queries, and I am using Cypress for testing. The problem is that these 2 dont seem to play well along. Apollo seems to be making all its requests through the Fetch API. But Cypress seems like it is not able to capture anything, except XHR requests. So what could I do to solve this problem? Is there a way for Cypress to capture "fetch" requests? Is there a way for react-apollo to use "xhr" instead of "fetch"? 回答1: Try out cypress-graphql-mock You just

Headers not showing in fetch response

和自甴很熟 提交于 2021-02-08 12:49:07
问题 I can see the header "x-auth-token" in Chrome DevTools. However, the header is not showing up in my fetch response. Please assist so I can use header data. I am using NodeJS as my backend API and ReactJS as my front-end. These are my files. NodeJS middleware - cors.js module.exports = function enableCorsSupport(app) { app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE"); res.header("Access

Disable cache for certain resource programmatically

痴心易碎 提交于 2021-02-08 10:14:58
问题 I have this setting in Chrome devtools: this setting works for me. However, I also want to disable the cache for certain resources. My question is - is there a way to disable the cache for a resource when you use fetch? fetch(url).then(v => {}); is there some option or header that we can use to prevent the browser from using the cache to retrieve the resource? 回答1: This has the answers I was looking for: https://hacks.mozilla.org/2016/03/referrer-and-cache-control-apis-for-fetch/ first make

Internal API fetch with getServerSideProps? (Next.js)

主宰稳场 提交于 2021-02-08 09:08:03
问题 I'm new to Next.js and I'm trying to understand the suggested structure and dealing with data between pages or components. For instance inside my home.js page, I fetch a internal API called /api/user.js where it sends back to me some user data from mongodb. The fetch right now happens inside the getServerSideProps() which passes various props to the page after some math. From my understanding this is the way to go for a good SEO, since props get fetched/modified server side and the page gets

Internal API fetch with getServerSideProps? (Next.js)

爷,独闯天下 提交于 2021-02-08 09:07:11
问题 I'm new to Next.js and I'm trying to understand the suggested structure and dealing with data between pages or components. For instance inside my home.js page, I fetch a internal API called /api/user.js where it sends back to me some user data from mongodb. The fetch right now happens inside the getServerSideProps() which passes various props to the page after some math. From my understanding this is the way to go for a good SEO, since props get fetched/modified server side and the page gets

Multiple fetch() with one signal in order to abort them all

折月煮酒 提交于 2021-02-08 07:19:30
问题 I saw this other answer: https://stackoverflow.com/a/47250621/2809729 So can I abort multiple fetch request using just one signal? 回答1: At the time I was writing the question, I already found the solution in this post on how to abort fetches from one of the pioneers that were working to the implementation of an abort. So yes you can :) Here a brief example: async function fetchStory({ signal }={}) { const storyResponse = await fetch('/story.json', { signal }); const data = await storyResponse

Multiple fetch() with one signal in order to abort them all

寵の児 提交于 2021-02-08 07:18:11
问题 I saw this other answer: https://stackoverflow.com/a/47250621/2809729 So can I abort multiple fetch request using just one signal? 回答1: At the time I was writing the question, I already found the solution in this post on how to abort fetches from one of the pioneers that were working to the implementation of an abort. So yes you can :) Here a brief example: async function fetchStory({ signal }={}) { const storyResponse = await fetch('/story.json', { signal }); const data = await storyResponse

React hooks to manage API call with a timer

余生长醉 提交于 2021-02-08 03:56:23
问题 I need to do a fetch API call that returns a URL, do something with the returned URL, then refresh the URL after 60 seconds. This is something I could comfortably achieve without hooks, but I'd like a hooks solution. IMPORTANT : I'm not looking to refactor this to multiple components, or create custom hooks for either the timer or API call. EDIT : The question is - is this the correct way to handle a timer in a hooks environment? Is there a better way? import React, { useState, useEffect }

Enabling gzip compression with fetch.js

懵懂的女人 提交于 2021-02-07 13:16:45
问题 I'm using fetch.js (https://github.com/github/fetch) to send a relatively large json object to the backend. The json is large in that it include an SVG image string. I'm not clear if fetch.js is using gzip compression by default, or if I need to manually compress and add headers. Any help would be appreciated. return new Promise((resolve, reject) => { fetch(api_base + "/api/save-photo", { method: 'POST', mode: 'cors', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify

Enabling gzip compression with fetch.js

眉间皱痕 提交于 2021-02-07 13:15:55
问题 I'm using fetch.js (https://github.com/github/fetch) to send a relatively large json object to the backend. The json is large in that it include an SVG image string. I'm not clear if fetch.js is using gzip compression by default, or if I need to manually compress and add headers. Any help would be appreciated. return new Promise((resolve, reject) => { fetch(api_base + "/api/save-photo", { method: 'POST', mode: 'cors', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify