Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Oct 3, 2024
1 parent b485972 commit 1c8ccb9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
13 changes: 6 additions & 7 deletions packages/nextra/src/client/components/auto-refresh.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react-hooks/rules-of-hooks, react-hooks/exhaustive-deps -- dev code will be removed from production build */
'use client'

// Based on https://www.steveruiz.me/posts/nextjs-refresh-content
Expand All @@ -7,16 +8,14 @@ import type { FC, ReactNode } from 'react'
import { useEffect } from 'react'

export const AutoRefresh: FC<{ children: ReactNode }> = ({ children }) => {
if (process.env.NODE_ENV === 'production') {
const port = process.env.NEXTRA_WS_PORT
console.log({ port })
if (process.env.NODE_ENV === 'production' || !port) {
return children
}

// eslint-disable-next-line react-hooks/rules-of-hooks -- this code will be removed from production
const router = useRouter()

// eslint-disable-next-line react-hooks/rules-of-hooks -- this code will be removed from production
useEffect(() => {
const ws = new WebSocket(`ws://localhost:${process.env.NEXTRA_WS_PORT}`)
const ws = new WebSocket(`ws://localhost:${port}`)
ws.onmessage = event => {
if (event.data === 'refresh') {
router.refresh()
Expand All @@ -25,7 +24,7 @@ export const AutoRefresh: FC<{ children: ReactNode }> = ({ children }) => {
return () => {
ws.close()
}
}, [router])
}, [])

return children
}
2 changes: 1 addition & 1 deletion packages/nextra/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env node */
import path, { sep } from 'node:path'
import { NextConfig } from 'next'
import type { NextConfig } from 'next'
import { fromZodError } from 'zod-validation-error'
import type { Nextra } from '../types'
import {
Expand Down
20 changes: 12 additions & 8 deletions packages/nextra/src/server/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'node:fs'
import fs from 'graceful-fs'
import { WebSocketServer } from 'ws'
import type { AddressInfo, WebSocket } from 'ws'
import { logger } from './utils.js'
Expand All @@ -14,18 +14,22 @@ function withResolvers<T>() {
return { promise, resolve: resolve!, reject: reject! }
}

// Based on https://www.steveruiz.me/posts/nextjs-refresh-content
// and https://github.com/gaearon/overreacted.io/pull/797
export async function createWsWatcherAndGetPort(
folder: string
): Promise<number> {
const { promise, resolve } = withResolvers<number>()

const wss = new WebSocketServer({ port: 0 }, function (this: {
address: () => AddressInfo
}) {
const { port } = this.address()
logger.info(`ws server is listening on port: ${port}`)
resolve(port)
})
const wss = new WebSocketServer(
// to get random port
{ port: 0 },
function (this: { address: () => AddressInfo }) {
const { port } = this.address()
logger.info(`ws server is listening on port: ${port}`)
resolve(port)
}
)

const clients = new Set<WebSocket>()

Expand Down

0 comments on commit 1c8ccb9

Please sign in to comment.