Skip to content

Remote Functions: deterministic paths #16285

Description

@theetrain

Describe the problem

Sometimes I want to write some internal-use APIs without building a full REST architecture. That way, I can send some cross-origin requests between the applications I own.

The use case that came up is managing logout logic between two applications:

On app.example.com there is a logout form:

<form method="POST" action="accounts.example.com/?/logout">
  <button>Log out</button>
</form>

And on accounts.example.com I can write a traditional SvelteKit Form Action to receive the cross-origin POST request in src/routes/+page.server.ts.

If a remote form named logout exists on accounts.example.com, it's not possible to rely on their path since it may change in between builds. Example generated path: ?/remote=q5s0im%2Flogout.

Describe the proposed solution

Some way to provide deterministic paths for remote form methods ahead of build time; especially if current workarounds may not be made available long-term.

Brainstorming some ways this can be exposed to SvelteKit developers:

  1. Remote functions defined anywhere in src/routes/**/*.remote.{ts|js} would generate a path the same way named Form Actions do today; and if there's a conflict with a same-named form action then there would be a build error.
    Example:
    // file: src/routes/auth.remote.ts
    
    import { form } from '$app/server'
    
    // exposed as `accounts.example.com/?/logout`
    export const logout = form(() => { /* do stuff */ }
  2. A third options parameter is provided for remote functions to set things like { url: '/api/logout' }
  3. A config option to make paths determined based on location in the project's file structure and method name, rather than a (seemingly) random hash being generated.
    Example: src/lib/auth.remote.ts#logout -> /_remote/lib/logout
  4. (cool, but understandably complex) A generated SDK for all remote functions in a given application. There may need to be considerations for 'private' functions (see Guarded / private prerender remote functions #15712)

It would be nice to have deterministic paths for query or command as well.

I understand the intent of Remote Functions and RPC in general is to not care too much about endpoint paths and to instead generate an SDK; which in SvelteKit's case is a build-time detail. At least, we could provide a simple escape hatch for these infrequent needs like a single method.

Alternatives considered

  • A named Form Action
  • A +server.ts POST endpoint

Importance

would make my life easier

Additional Information

This relates to another feature request that would also require deterministic paths:

Especially if the 'generated SDK' idea is pursued.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions