nhsuk/nhsuk-prototype-kit

Allow autoStoreData to work on the root page

Opened this issue · 6 comments

The regex here is enabling autoStoreData functionality on all pages except the home page /.
This should be changed to all pages including homepage.

app.post(/^\/([^.]+)$/, (req, res) => {


Edit:
I didn't really word this properly. I should have said:
The regex here is handling the POST > GET redirect which is necessary for the autoStoreData functionality on all pages except the home page /.

is it not working for you?

that line is about POST > GET redirect. The autostoredata line is here, and works on all paths:

https://github.com/nhsuk/nhsuk-prototype-kit/blob/main/app.js#L96

Even the line you highlighted for redirects looks like it works on /, it might not work on root without the slash, is that what you're trying to do?

The regex doesn't match /. It only matches / followed by one or more characters.

> "/".match(/^\/([^.]+)$/)
null
>
> "/home/".match(/^\/([^.]+)$/)
[ '/home/', 'home/', index: 0, input: '/home/', groups: undefined ]

In fact, if you want to just redirect all POST to GET while keeping the URL the same, there's no need for any regex:

app.post("*", (req, res) => {
  res.redirect(req.originalUrl);
});

I'll try this out later and submit a PR

oh yeh sorry you're right, at least one char:
https://regexper.com/#%5E%5C%2F%28%5B%5E.%5D%2B%29%24

the original seems to not want to match a . but I don't know why (I wrote it)

If you feel like it you can do a PR to the GOV.UK kit:
https://github.com/alphagov/govuk-prototype-kit/blob/main/server.js#L165

and I see we have a fix that isnt in the NHS kit - it retains query strings