generated from yandex-praktikum/client-server-template-with-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
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
SSR prod и dev режимы #59
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ffd1a7c
добавил ssr в дев и прод режимы
DNLHC 198b2e1
сборка клиента для корректного линтинга проекта
DNLHC c508f61
добавил команду build:ssr
DNLHC 2765af0
обработка стора на SSR
DNLHC fa0f227
добавил доступ к стору в лоадерах роутов
DNLHC f7b7333
сервер порт 3001 -> 5000 для совместимости с oauth
DNLHC bcf0ceb
добавил комплицию типов пакета client
DNLHC 4eb4c79
client-only перевод в jsx
DNLHC 4084e4b
мелкие правки
DNLHC 6bbc464
добавил rimraf для кроссплатформенности
DNLHC afe6be2
замена `querySelector`
DNLHC 16dfd95
подготовка к мерджу
DNLHC bae89c3
Merge branch 'main' into feat/ssr/38
DNLHC 4661b5d
экспорт тип leaderboard стейта, исправление в условии SSR
DNLHC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
VITE_API_BASE_URL=http://localhost:5000/api/v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,29 @@ | ||
import { AuthLayout, getCurrentUser } from 'components/auth-layout'; | ||
import { | ||
Route, | ||
defer, | ||
createBrowserRouter, | ||
createRoutesFromElements, | ||
RouterProvider, | ||
} from 'react-router-dom'; | ||
import { ProtectedRoutes } from 'utils/protected-routes'; | ||
import { Profile } from 'pages/profile'; | ||
import { MainPage } from 'pages/main'; | ||
import { HowToModal } from 'components/how-to-modal'; | ||
import { GamePage } from 'pages/game'; | ||
import { RouterPaths } from './app.types'; | ||
import { LoginPage, RegisterPage } from 'pages/auth'; | ||
import { MainLayout } from 'components/main-layout'; | ||
import { Error404, Error500 } from 'pages/error'; | ||
import { Leaderboard } from 'pages/leaderboard'; | ||
import { ForumThread } from 'pages/forum-thread'; | ||
import { NewThreadModal } from 'components/modal-new-thread'; | ||
import { Forum } from 'pages/forum'; | ||
import { ThemeProvider, Theme } from 'components/hooks/use-theme'; | ||
import React from 'react'; | ||
import { Provider as StoreProvider } from 'react-redux'; | ||
import type { AppStore, InitialState } from './store'; | ||
import './styles/index.pcss'; | ||
|
||
const router = createBrowserRouter( | ||
createRoutesFromElements( | ||
<Route | ||
element={<AuthLayout />} | ||
loader={() => defer({ userPromise: getCurrentUser() })} | ||
> | ||
<Route path={RouterPaths.REGISTER} element={<RegisterPage />} /> | ||
<Route path={RouterPaths.LOGIN} element={<LoginPage />} /> | ||
<Route element={<MainLayout />}> | ||
<Route path={RouterPaths.MAIN} element={<MainPage />}> | ||
<Route path={RouterPaths.HOW_TO} element={<HowToModal />} /> | ||
</Route> | ||
<Route path={RouterPaths.LEADERBOARD} element={<Leaderboard />} /> | ||
<Route path={RouterPaths.FORUM} element={<Forum />}> | ||
<Route path={RouterPaths.NEW_THREAD} element={<NewThreadModal />} /> | ||
</Route> | ||
<Route path={`${RouterPaths.FORUM}/:id`} element={<ForumThread />} /> | ||
<Route element={<ProtectedRoutes />}> | ||
<Route path={RouterPaths.PROFILE} element={<Profile />} /> | ||
<Route path={`${RouterPaths.GAME}/:id`} element={<GamePage />} /> | ||
</Route> | ||
</Route> | ||
<Route path={RouterPaths.ERROR_500} element={<Error500 />} /> | ||
<Route path={RouterPaths.ERROR_404} element={<Error404 />} /> | ||
<Route path="*" element={<Error404 />} /> | ||
</Route> | ||
) | ||
); | ||
|
||
function App() { | ||
return <RouterProvider router={router} />; | ||
function App({ | ||
children, | ||
theme = Theme.LIGHT, | ||
store, | ||
initialState, | ||
}: { | ||
children: React.ReactNode; | ||
theme?: string; | ||
store: AppStore; | ||
initialState?: InitialState; | ||
}) { | ||
return ( | ||
<React.StrictMode> | ||
<ThemeProvider initialValue={theme}> | ||
<StoreProvider store={store} serverState={initialState}> | ||
{children} | ||
</StoreProvider> | ||
</ThemeProvider> | ||
</React.StrictMode> | ||
); | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
declare const __SERVER_PORT__: number; | ||
|
||
declare global { | ||
interface Window { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
__INITIAL_STATE__?: string; | ||
} | ||
} | ||
|
||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/client/src/components/client-only/client-only.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
export const ClientOnly = ({ children }: { children: React.ReactNode }) => { | ||
const [hasMounted, setHasMounted] = useState(false); | ||
|
||
useEffect(() => { | ||
setHasMounted(true); | ||
}, []); | ||
|
||
if (!hasMounted) return null; | ||
|
||
return <>{children}</>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ClientOnly } from './client-only'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А этот код разве не отрабатывает раньше, чем
useEffect
? Это метод работает корректно весь?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Отрабатывает раньше, в этом и задумка. Это обёртка для компонентов, которые не должны рендериться на сервере.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
И как он узнает, что он не на сервере, если у него всегда
hasMounted === false
? На клиенте он так же получитfalse
, я не вижу никаких других зависимостей в компоненте, чтобы дойти до рендера компонентовThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
На клиенте сработает useEffect хук после первого монтирования,
hasMounted
станетtrue
(7 строчка), компонент обновится и отрендеритchildren
.На сервере компонент не смонтируется и вернёт
null
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
оки