sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.74k stars 1.95k forks source link

Options for Path-Based Trailing Slash #12610

Open XIYO opened 2 months ago

XIYO commented 2 months ago

Describe the problem

Now, let's discuss navigation formats.

Going a bit deeper:

When you want to control screen tree access with relative paths, routes composed of slugs might not allow for precise trailing slashes and may require using ignore (which could potentially cause issues in the future).

Describe the proposed solution

export function trailingSlash({ page }) {
  const isDirectory = isDirectory(page.url);
  // or
  // const isCategoryPage ~~

  return isDirectory ? 'always' : 'never';
}

Importance

would make my life easier

Conduitry commented 2 months ago

https://kit.svelte.dev/docs/page-options#trailingslash You can put export const trailingSlash = whatever to have a custom value in each layout and on each page. Does this get you what you're asking for?

XIYO commented 2 months ago

I’m referring to the expansion of this feature.

Given a route structure like /src/route/[...slug], where the following paths are all routed by this setup:

I’m requesting more granular control over trailing slashes for each URL.

Currently, trailing slashes cannot be customized for individual URLs when using dynamic routes like [slug] or [...slug].

dominikg commented 2 months ago

it should be consistent by route I think. If some dynamic values have a need for different trailing, can you use param matchers to discriminate? [slug=slugNoTrailing]
[slug=slugWithTrailing]

XIYO commented 2 months ago

My initial example had a conceptual flaw.

Before proposing any code, I believe it’s important to first assess whether my suggestion addresses a valid requirement and, if deemed valuable, then proceed to propose a code solution. As a result, my initial code proposal was inadequate.

Initial Proposal with Conceptual Flaw:

export function trailingSlash({ page }) {
  const isDirectory = isDirectory(page.url); // Adding a trailing slash to an already determined URL seems like a poor requirement.
  // or
  // const isCategoryPage ~~

  return isDirectory ? 'always' : 'never';
}

What I intended to convey was the idea that URLs should be treated similarly to file system paths, but my initial code failed to express this concept correctly.

Here is a more refined version:


Typical Web File Serving

When serving with the html file extension included: /posts/category-a/my-cat.html is exposed. From this, it's immediately clear that it's a file.

When serving without the html extension: The URL could be hosted as /posts/category-a/my-cat or /posts/category-a/my-cat/.

This creates ambiguity in interpreting the path. Does "my-cat" refer to a category or a specific post?

My route structure is as follows:

- root
  - src
    - route
      - posts
        - [...slug]

In this structure, it’s not possible to dynamically change the trailing slash. While this might be irrelevant to users, it provides value to developers by making relative paths in anchors more meaningful.

Here’s an improved approach:

+page.js, +page.server.js:

export const trailingslash = ({ data }) => { // Needs more specific information regarding the passed data
  const isDirectory = data.type === 'D' ? true : false;

  return isDirectory ? 'always' : 'never';
}

+page.svelte:

<script>
  import { page } from 'app/store';
  // or
  // const { data } = $props();
</script>

<a href={$page.data.type === 'D' ? './' : '../'}>
Go up
</a>

// or

{#if $page.data.type !== 'D'} // current page is a post
  <a href='./'>Go to categories</a>
{/if}