.client
模組雖然不常見,但您可能會有在瀏覽器中使用模組副作用的檔案或相依性。您可以使用檔案名稱上的 *.client.ts
或將檔案巢狀於 .client
目錄中,以強制它們離開伺服器捆綁包。
// this would break the server
export const supportsVibrationAPI =
"vibrate" in window.navigator;
請注意,從此模組匯出的值在伺服器上都將是 undefined
,因此唯一可以使用它們的地方是在 useEffect
和使用者事件(例如點擊處理程序)。
import { supportsVibrationAPI } from "./feature-check.client.ts";
console.log(supportsVibrationAPI);
// server: undefined
// client: true | false
.client
目錄。Classic Remix Compiler 僅支援 .client
檔案。
有關更多資訊,請參閱側邊欄中的「路由模組」部分。