Skip to content

fix(start): fixed route scripts filtering - #3082

Merged
schiller-manuel merged 6 commits into
TanStack:mainfrom
3fuyang:fix/scripts-filtering
Dec 28, 2024
Merged

fix(start): fixed route scripts filtering#3082
schiller-manuel merged 6 commits into
TanStack:mainfrom
3fuyang:fix/scripts-filtering

Conversation

@3fuyang

@3fuyang 3fuyang commented Dec 27, 2024

Copy link
Copy Markdown
Contributor

The filtering before is like:

// ...
select: (state) => {
  // Assume here:
  // `[[undefined], undefined]`
  const scriptsOfEachMatch = state.matches.map((match) => match.scripts)
  // Filtered:
  // `[[undefined]]`
  const truthyScriptsOfEachMatch = scriptsOfEachMatch.filter(Boolean)
  // Turns out to be:
  // `[undefined]`
  // which will cause `TypeError` when `undefined` gets destructured below
  const truthyScripts = truthyScriptsOfEachMatch.flat(1)

  return {
    scripts: truthyScripts.map(({ children, ...script }) => ({ // <- TypeError: Cannot destructure property 'children' of 'undefined' as it is undefined.
      // ...
    })),
  }
},
// ...

Which will cause error when specifying scripts like:

export const Route = createRootRoute({
  head: () => ({
    // ...
    scripts: [undefined], // Passes type check but will panic at runtime
  }),
  // ...
})

This PR moves the filter(Boolean) after flattening. But I'm not sure if the following patterns are equivalent:

arr.filter(Boolean).flat(1).filter(Boolean) // Semantically correct

arr.flat(1).filter(Boolean) // Less code

@schiller-manuel

Copy link
Copy Markdown
Collaborator

we need an e2e test for this

@3fuyang

3fuyang commented Dec 27, 2024

Copy link
Copy Markdown
Contributor Author

Hi @schiller-manuel, it seems the existing e2e are all related to examples, so I'm not sure where to put the e2e case for this. But I found the start package already includes unit tests for scripts, so I added it there instead. PTAL

@nx-cloud

nx-cloud Bot commented Dec 27, 2024

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit 4fc30eb.

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 5m 6s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 42s View ↗

☁️ Nx Cloud last updated this comment at 2024-12-28 20:21:19 UTC

@schiller-manuel

Copy link
Copy Markdown
Collaborator

I would put an e2e test in this project: https://github.com/TanStack/router/tree/main/e2e/start/basic

add new routes for this setup and then access them via the playwright test and add some assertions

@pkg-pr-new

pkg-pr-new Bot commented Dec 27, 2024

Copy link
Copy Markdown

Open in Stackblitz

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@3082

@tanstack/create-router

npm i https://pkg.pr.new/@tanstack/create-router@3082

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@3082

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@3082

@tanstack/react-cross-context

npm i https://pkg.pr.new/@tanstack/react-cross-context@3082

@tanstack/create-start

npm i https://pkg.pr.new/@tanstack/create-start@3082

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@3082

@tanstack/react-router-with-query

npm i https://pkg.pr.new/@tanstack/react-router-with-query@3082

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@3082

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@3082

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@3082

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@3082

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@3082

@tanstack/start

npm i https://pkg.pr.new/@tanstack/start@3082

@tanstack/start-vite-plugin

npm i https://pkg.pr.new/@tanstack/start-vite-plugin@3082

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@3082

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@3082

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@3082

commit: 4fc30eb

@3fuyang

3fuyang commented Dec 28, 2024

Copy link
Copy Markdown
Contributor Author

Hi @schiller-manuel , thanks for the guidance!

I've added a new /scripts route and the corresponding case to the start/basic example. Here are a few tips for review:

  • Two empty script files script.js and script2.js are added to public for testing.
  • I have to include the added script files to TSConfig.include so that it does not conflict with the global ESLint configuration for typed linting.
  • I found that a case was marked with only, thus only this case gets evaluated when testing. I removed the only declaration and please let me know if this was intentional.
  • The invoking a server function with custom response status code case fails when running e2e tests, so I modified the assertion to make it pass.

BTW please let me know whether I should remove the UT added to the start package.

@3fuyang

3fuyang commented Dec 28, 2024

Copy link
Copy Markdown
Contributor Author

Oops, the test seems to behave differently between headless and UI mode, ill check it

Update: Replaced the assertion with toHaveCount() to avoid inconsistency between headless and UI mode.

@schiller-manuel

Copy link
Copy Markdown
Collaborator

very nice, thanks a lot!

@schiller-manuel
schiller-manuel merged commit ec7b40f into TanStack:main Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants