October 2022 Summaries
4 posts from Clerk
Filter
Month:
Year:
Post Summaries
Back to Blog
Next.js middleware is highly versatile but by default tends to run on more requests than needed, which can include unnecessary static assets and potentially disrupt your application. A simple way to observe this is by adding a logger to your middleware, which will reveal multiple requests to static files when loading a new Next.js application. To optimize, you can configure a matcher using regular expressions to ensure middleware only runs on dynamic files by excluding those with a period in their path. This approach reduces middleware invocation to just one request, but as your application grows, you might need to adjust it to include or exclude specific paths or assets. For example, you can include certain static assets like favicon.ico by defining additional matchers or exclude specific paths such as API routes by expanding the regular expression or using conditional statements, though the latter might inefficiently use compute resources. This foundational setup allows for a more efficient use of middleware in Next.js applications.
Oct 06, 2022
440 words in the original blog post.
This week, a recurring debate on Hacker News about using JSON Web Tokens (JWTs) as session tokens resurfaced, highlighting divergent views on their efficacy and security. The consensus among participants is that infrequently-refreshed, stateless JWTs are unsuitable as session tokens due to their irrevocability, and combining long-lived JWTs with a stateful database-backed blocklist is redundant. However, frequently-refreshed, stateless JWTs are cautiously accepted due to their short expiration acting as a revocation mechanism, although database-backed, stateful session tokens are easier and sufficient for most cases. Despite the complexity and minimal benefits of frequently-refreshed JWTs for most applications, their usage is rising due to trends emphasizing efficiency and the need for synchronized sessions in modern software integrations. Proponents argue for embracing JWTs by improving their implementation, particularly by ensuring short expiration times to enhance security against XSS attacks. The discussion underscores the importance of optimizing speed and integration capabilities while addressing security concerns in web development.
Oct 05, 2022
738 words in the original blog post.
Next.js middleware lacks a built-in mechanism for passing values across different parts of an application, but it offers a feature called "rewrites" that can be utilized to modify request metadata and pass data. This involves rewriting requests to the same URL while incorporating desired data into the request's metadata, which can then be accessed via API routes or getServerSideProps. However, inconsistencies across different runtimes can pose challenges in implementing this process. The provided code snippets offer solutions to mitigate these issues, using functions to set and retrieve context data securely while preventing spoofing through pre-defined keys. Limitations include the need for all key/value pairs to be strings due to restrictions in headers and query strings, and potential constraints on the total length of these elements by hosting providers like Vercel.
Oct 05, 2022
845 words in the original blog post.
Version 4.5 of @clerk/nextjs introduces significant enhancements to server-side authentication in Next.js, notably shifting authentication processes to middleware, which aligns with practices in frameworks like Express and Ruby on Rails. This update simplifies the authentication workflow by allowing developers to authenticate requests once in middleware, enabling seamless access to authentication states in API routes and server-side rendering functions. The new isomorphic getAuth() helper supports both node and edge runtimes introduced in Next.js 12.2, enhancing flexibility. Additionally, the developer experience is improved with unified server-side import paths and the elimination of wrapper functions in endpoint-specific code. The changes are designed with future Next.js Layouts support in mind, facilitating shared authentication states across parallel-loading layout files. Developers are encouraged to explore these updates through the provided guides and community resources.
Oct 04, 2022
484 words in the original blog post.