sveltejs / sveltejs/kit

Better documentation / examples / utilities for working with query params and named form actions

Open
#10,585 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

Using the default action persists current query params and sends them to the server.

The documentation for named form actions describes "adding a query parameter", however, the examples provided replace any query parameters currently present on the page with a new query string containing only the action name. To preserve the current query param and also add an action name, I've had to do the following:

<form action="{$page.url.search ?? ''}{$page.url.search ? '&' : '?'}/saveExposure" ...>

This probably won't work in the example where a form with a name action is submitted and then another form with a named action is submitted on the resulting page. In that case, two named actions will be present in the query string, since the named action is not removed on submission success.

A utility to add or replace the current named action in the current page's query params would be extremely useful.

Similarly, in the action handler, redirecting from a form submission and preserving the query params, minus the named action, requires something like this to get a clean query param string for use in building a redirect url:

export function removeNamedActionParam(params: URLSearchParams) {
  const returnedParams = new URLSearchParams(params);
  for (const param of params) {
    if (param[0].startsWith("/")) {
      returnedParams.delete(param[0]);
    }
  }
  return `?${returnedParams.toString()}`;
}

It would be great to have this (or something like this) provided in the sveltekit library.

Describe the proposed solution

Utilities for adding and removing named action params from query strings, or documentation on how to do so. The current documentation assumes that named actions and query parameters will never be used at the same time.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked named form actions documentation and review its examples alongside the reported query-parameter behavior. Done means documenting or providing utilities that preserve existing parameters while adding an action and remove named action parameters when building redirects.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
documentation, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.