roxiness / routify

Automated Svelte routes
https://routify.dev
1.88k stars 88 forks source link

Route parameters capture too much: `test[slug].svelte` slug is "testfoo" not "foo" #246

Open tv42 opened 4 years ago

tv42 commented 4 years ago

routify 1.9.7

create src/pages/test[slug].svelte:

<script>
 import { params } from '@sveltech/routify'

 export let slug;
</script>

<p>slug {JSON.stringify(slug)}</p>

<p>params {JSON.stringify($params)}</p>

visit /testfoo, observe output

slug "testfoo"

params {"slug":"testfoo"}

Expected "foo" for both.

jakobrosenberg commented 4 years ago

Currently route nodes are either static or parameterized. That's not to say flexibility can't be added to the nodes, just that I haven't come across a justifying use case.

tv42 commented 4 years ago

For what it's worth, out of curiosity I tested the same with Sapper and it works as expected there (though with a lot more bureaucracy in handing the data over):

<script context="module">
    export function preload(page, session) {
        const { slug } = page.params;

        return { slug };
    }
</script>

<script>
  export let slug;
</script>

<p>slug {JSON.stringify(slug)}</p>
mathiasrw commented 3 years ago

It seems counterintuitive to force the sue deeply nested single files if you have several parameters (like when navigating user/[userId]/order/[orderID]/product/[productID]/review.svelte)

Would be lovely to have a file test[slug].svelte to correspond to test/[slug].svelte. I suggest that an error is provided if you have both the folder structure and the file naming structure.

tv42 commented 3 years ago

I don't like the idea of magic slash that sometimes is there sometimes isn't. test/[slug].svelte should match test/foo but not testfoo; test[slug].svelte should match testfoo but not test/foo.

jakobrosenberg commented 3 years ago

I'd like to support artificial paths. This is an issue I come across myself from time to time.

Not sure what the best syntax would be for the following examples.

  1. Nesting statics

    helpful
    explanatory
    path
      index.svelte  
  2. Nesting dynamics

    [year]
    [month]
    [day]
      index.svelte
  3. Nesting both

    
    year
    [year]
    month
      [month]
        day
          [day]
            index.svelte

### Option 1: Treat dynamics as folders

statics: helpful[]explanatory[]path.svelte dynamics: [year][month][day].svelte both: year[year]month[month]day[day].svelte



**Pros**
1. It's clean
2. Dynamics-are-treated-as-folders is an easy concept to follow

**Cons**
1. It prevents this type of syntax `[year]-[month]-[day].svelte`.
2. Not sure how I feel about nesting statics.
  a. Alternatively we can also fake a nested structure of statics by adding a separate folder delimiter.
  b. Having both a folder delimiter and "auto" folders is confusing.

### Option 2: Create a folder delimiter
What's a good delimiter? It has to be safe across OS'es and unsafe in URLs. We don't want safe URL characters as they could conflict with actual URLs.
mathiasrw commented 3 years ago

It is actually true - the [year]-[month]-[day].svelte syntax could be usefull.

| is a great delimiter in my opinion... but also a bit of a cumbersome typing to have year|[year]|month|[month]|day|[day].svelte

May I suggest that

Examples:

statics:   helpful|explanatory|path.svelte
dynamics:  [year][month][day].svelte
both:      year[year]month[month]day[day].svelte
Specific   [year]-[month]-[day]|.svelte 
jakobrosenberg commented 3 years ago

I thought | pipes were illegal characters, but I could easily be wrong.

tv42 commented 3 years ago

Can we please not contaminate this issue with magic slash alternatives?

jakobrosenberg commented 3 years ago

I'm not sure where else to address how filenames should be parsed. Any aspect affected by a solution should be considered.

Please note, I'm not campaigning for magic slashes, just trying to provide an overview of the different options that are available for filename parsing.