Closed iammatthias closed 6 years ago
Hi Matthias! With the latest version of gatsby-plugin-offline, we serve most pages from the service worker so they can work offline - see the regexps below:
To prevent the service worker from handling form submissions, add ?no-cache=1
to the URL - for example:
fetch("https://iamv2.iammatthias.com/contact?no-cache=1", {
"body": encode({ 'form-name': 'contact', ...this.state })
});
I've just sent a test message using code similar to the above, please let me know if you got it! Leaving this open for discussion of how we should handle fetch
with the service worker
Thanks, @davidbailey00! I'll give that a try right now and report back!
@davidbailey00 YES! Thank you so much. Form submissions are coming in properly now, just tested in Chrome and Firefox.
Glad to hear! Btw, with the line of code you changed, /contact?no-cache=1
should work fine if you don't want to write out the whole URL (i.e. https://iamv2.iammatthias.com/contact?no-cache=1
).
As mentioned earlier, I'll leave this open for discussion about what the default behaviour should be - might play around tomorrow and see if I can improve this.
Can we default to not caching POST requests?
Can we default to not caching POST requests?
@m-allanson unfortunately Workbox doesn't allow configuring this - we can either enable serving an offline shell and have this as a side-effect, or only guarantee that the homepage works offline. Or as a third option we can force Gatsby to fetch the HTML of each page so that it's cached at runtime, but this would waste bandwidth. (See https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config, especially navigateFallback
)
Maybe it would be best to add documentation to gatsby-plugin-offline
giving an example of how to modify navigateFallbackBlacklist
to prevent handling API requests? I'd can submit a PR with this if that sounds good!
This seems to have fixed itself, possibly due to some changes with Workbox - @iammatthias could you try upgrading your packages and see if the form works now without ?no-cache=1
? If so, I'll close the issue.
@davidbailey00 I'll give this a try right now and report back.
@davidbailey00 Works perfectly. Tried in Safari, Chrome, and Firefox.
@iammatthias awesome! I'll close this issue then :)
@davidbailey00 I have noticed this issue again using Gatsby v2.0.22
. Not sure when the issue was reintroduced but I am reopening this ticket.
I also confirmed that the workaround of ?no-cache=1
still works.
@ryanwiemer Thanks for pointing this out, and sorry for the late response - I'll try and take a look tomorrow to see what's going on.
I think I understand what's going wrong here - if I do a POST to any page other than the homepage, it correctly fetches from the server - this returns a 404 since Netlify only serves pages over GET requests:
However, the homepage (/index.html
) is explicitly cached rather than defaulting to the offline shell, resulting in different behaviour:
To fix this we need to change the behaviour of the offline plugin, so that the index page isn't explicitly cached - it doesn't need to be, since its resources will be cached automatically once the SW installs, so there's no good reason to do this really.
https://www.cairnsbeachresort.com.au/contact/ Hey guys, I ran into the same issue too with this page. I added "?no-cache=1" but still not working :( Can anyone please take a look? Cheers.
<form name="cbr-contact-form" method="post" data-netlify="true" action="/contact?no-cache=1"
<label className="block text-grey-darker font-bold mb-2">
Your Email:
</label>
<input type="email" name="email" className="text-input" />
<label className="block text-grey-darker font-bold mb-2">
Your Mobile/Landline Number:
</label>
<input type="text" name="phone" className="text-input" />
<label className="block text-grey-darker font-bold mb-2">
Your Message:
</label>
<textarea name="message" className="text-input h-48" />
<button type="submit" className="btn">
Submit
</button>
</form>
I'm having this issue as well. None of the recommended fixes have worked for me.
I confirm this is still an issue. Its working fine without /?no-cache=1
in Chrome but not in Safari. Fortunately adding /?no-cache=1
to fetch URI does the job. Thank you.
Description
Source: https://github.com/iammatthias/net/blob/v2/src/components/Info/ContactForm.js
Netlify form configuration works in Gatsby V1, but not in Gatsby V2. The form submission presents as successful, but does not record. Live example: https://iamv2.iammatthias.com/contact
Logging the encoded form submissions on the
Submit
function usingconsole.log(encode({ 'form-name': 'contact', ...this.state }))
. Encoded value is logged to console asform-name=contact&name=test&email=test%40test.com&message=test&showModal=false
.FETCH request seems to be handled by Service Worker, does not go to Netlify. Also presents with empty body. Appears to use HTML/1.1 instead of HTML/2.
Form configuration works as expected in site built with Gatsby v1. Live Example / Source
Environment
Local
Production - Netlify