Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use routes constant for all internal paths #1071

Merged
merged 13 commits into from
Oct 26, 2022

Conversation

evgenyboxer
Copy link
Collaborator

@evgenyboxer evgenyboxer commented Oct 21, 2022

Hopefully I've covered all instances of the internal paths! I've replaced them all with the routes constant.

@vercel
Copy link

vercel bot commented Oct 21, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
gallery ✅ Ready (Inspect) Visit Preview Oct 26, 2022 at 2:02PM (UTC)

@evgenyboxer evgenyboxer changed the title [wip]: refactor: use routes constant for all internal paths refactor: use routes constant for all internal paths Oct 21, 2022
src/constants/routes.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@Robinnnnn Robinnnnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based

@tbezman
Copy link
Collaborator

tbezman commented Oct 21, 2022

@evgenyboxer Have you looked into this? https://github.com/ckastbjerg/next-type-safe-routes

Seems like it could get us most of the benefits without any of the maintenance.

Also would get us type safety on params and stuff.

Definitely curious to hear your thoughts. Let me know!

@evgenyboxer
Copy link
Collaborator Author

evgenyboxer commented Oct 22, 2022

Hey @tbezman

That's absolutely the way to go, had no idea this existed. My only concern with this specific library is that it doesn't look widely used / maintained.

I had a spin on it, and found a couple of issues -

  1. It seems that they require an api folder to be present inside the pages (took me a little while to debug to reach this conclusion) and this is reproduced in their example repo as well. Workaround is to create an empty api folder and then it would generate the types. However, it generates them with TypeSafeApiRoute which is essentially assigned to any and that kinda breaks the type safety if you directly use their getRoute (caused by the empty api folder). I find it odd that they require an api folder... but anyways, if they would at least not generate TypeSafeApiRoute it would work well in this case. We could create a new getSafeRoute that just uses the TypeSafePage type without TypeSafeApiRoute.

  2. When using with Next.js v12 there are some warnings coming out of the next.js config, but still it generates the types fine.

  3. You can't change the location of where the types are generated, seems there is a PR opened from March but wasn't merged - Configurable output directory  ckastbjerg/next-type-safe-routes#35

Happy for you to play with this as well and let me know if you find success in using it.

I really like the direction of this library, it is definitely an improvement over my solution.


const moduleExports = {
/** @type {import('next').NextConfig} */
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives a nice autocomplete for next config options

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YESSSSSS

module.exports = withSentryConfig(moduleExports, sentryWebpackPluginOptions);
const plugins = [withRoutes, (config) => withSentryConfig(config, sentryWebpackPluginOptions)];

module.exports = () => plugins.reduce((config, plugin) => plugin(config), nextConfig);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed the thread here - cyrilwanner/next-compose-plugins#59

<ButtonLink href="#">primary</ButtonLink>
<ButtonLink href="#" pending>
<ButtonLink href={{ pathname: '/' }}>primary</ButtonLink>
<ButtonLink href={{ pathname: '/' }} pending>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbezman Would that work? or do we need a workaround here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem here

? { pathname: '/[username]', query: { username: admire.admirer.username } }
: { pathname: '/' };

const admirerLink = route(admirerLinkRoute);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using this pattern here since <Link wants a Route and AdmirerName wants a string.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. Next 13 removes this double tag nonsense so we'll be able to just use the route!


export type InternalAnchorElementProps = Omit<AnchorElementProps, 'href'> & {
href: Route;
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbezman I wasn't sure of a good name for this, so went with something generic (Elements.ts) that could house similar types in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dig it.


const moduleExports = {
/** @type {import('next').NextConfig} */
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives us a new autocomplete for the config

@tbezman tbezman merged commit dbd69ce into main Oct 26, 2022
@tbezman tbezman deleted the refactor/add-routes-constant branch October 26, 2022 14:08
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.

3 participants