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

Improve dom types for server side rendering environments #53971

Open
5 tasks done
RJWadley opened this issue Apr 23, 2023 · 2 comments
Open
5 tasks done

Improve dom types for server side rendering environments #53971

RJWadley opened this issue Apr 23, 2023 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@RJWadley
Copy link

RJWadley commented Apr 23, 2023

Suggestion

πŸ” Search Terms

DSG, SSG, SSR, Server Side Rendering, NextJS GatsbyJS React, DOM type definitions, environment tsconfig, lib.dom

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I envision this as an option for lib:

{
  "compilerOptions": {
    "lib": ["dom.ssr", "DOM.Iterable"]
  }
}

that would load any DOM apis optionally, as if window was typed as Window | undefined, which would require developers to include type checks when accessing those apis.

Typescript might also be able to check for "use client" directives to narrow the type of window automatically.

πŸ“ƒ Motivating Example

Catch errors that could cause server side rendering to fail!

Somewhere in your react code, you may write:

// accessing browser apis and constants could cause uncaught runtime errors!
const lastLogin = localStorage.getItem("lastLogin"); // <β€” 'localStorage' is possibly 'undefined'
const resolution = devicePixelRatio; // <β€” 'devicePixelRatio' is possibly 'undefined'
const screenWidth = window.innerWidth // <β€” 'window' is possibly 'undefined'

// correct usage, with type checks
const lastLogin = isBrowser() && localStorage.getItem("lastLogin");
const resolution = typeof devicePixelRatio === "number" ? devicePixelRatio : 1
const screenWidth = typeof window !== "undefined" && window.innerWidth;

πŸ’» Use Cases

As server side rendering is growing in popularity, web application code needs to safely run in both browser and node environments. Accidentally accessing DOM apis without checking them first is a common source of crashes in these types of apps. Debugging these issues is often more time consuming than it should be, and requires reading logs to determine what went wrong.

Currently, the best approach I've found is to use eslint-plugin-ssr-friendly to catch errors, but this sort of approach only catches surface level errors. (It's entirely based on scope. i.e. you can never use globals in some scopes even with type checks, and you can always use globals in other scopes even if they're dangerous).

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Apr 25, 2023
@lanwin
Copy link

lanwin commented Apr 4, 2024

Wouldnt it be a good idea to allow to just use globalThis in this case where globalThis.innerWidth is number|undefined but innerWidth.window.innerWidth is number but innerWidth.window is Window|undefined.

@fabb
Copy link

fabb commented Nov 2, 2024

The global navigator object would need to have a special type too because node.js 21 introduced it too with only a subset of available keys: nodejs/node#50269

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants