0

First and foremost I would like to inform you that I am new to node js!

I have been asked to implement a simple application in node js. So, I have created a form that submits via POST method its data. When data are retrieved and processed I render another page in order to display some info regarding the submitted data and the results of the whole process.

My problem is that users are able to click the back button from the browser, load the submitted data again and re-press the submit button.

It sounds stupid but I'am stuck in here for days...

Thanks in advance.

John
  • 1
  • If I got it correctly, you want to clear form fields when user presses the back button. If that is the case, check this: https://stackoverflow.com/questions/8861181/clear-all-fields-in-a-form-upon-going-back-with-browser-back-button – amiraliamhh Jun 15 '18 at 11:31

2 Answers2

0

This is due to HTTP caching strategies by the various browsers: “back-forward caching” (BFCache) in FireFox; "Page Cache" in Safari, etc. It was implemented to optimize navigating speeds. Essentially it caches the entire page’s DOM, including form elements for non HTTPS sites. The form elements can be cleared programmatically by listening to events for loading a page or by using HTTP headers from the server to not cache that page (e.g. Cache-Control: no-cache, max-age=0, must-revalidate, no-store).

To set the Cache-Control header for a response in Express use the set method for the response object:

res.set('Cache-Control', 'no-cache, max-age=0, must-revalidate, no-store');
Jason Cust
  • 10,743
  • 2
  • 33
  • 45
  • Thank you for your detailed answer. I put the line: res.set('Cache-Control', 'no-cache, max-age=0, must-revalidate, no-store'); right before then render command and as I saw the Response headers in the developer tools the cache-control property has the expected values. However, it doesn't seem to work.. At least in my case, as I still can click the back button and the have access to the posted values. – John Jun 15 '18 at 13:22
0

I managed to solve my problem by adding the property: autocomplete="off" in the form.

Thank you for your answers.

John
  • 1