-
Notifications
You must be signed in to change notification settings - Fork 147
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
Add Server-Side Rendering (SSR) #306
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
edc21bf
[WIP] Add SSR
zaaack a2d28fa
Using nuget packages
zaaack 8a432db
Add ignore client files for dotnet watch
zaaack a9064c8
Small fixes
zaaack f948f8d
Remove package-lock.json
zaaack 220f1b4
Refactor RunSSR
zaaack 041a158
Fix review
zaaack 16ba0de
Fix wishList
zaaack 359c19e
Fix Images case
zaaack 6a5e042
Let client handle 404 page
zaaack 094f0e6
Fix typo
zaaack 1f9bbc1
Upgrade Fable.Elmish.React to 1.0.2
zaaack b200e88
Remove cookies
zaaack 6e2acb7
Fix review
zaaack 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
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,47 +1,31 @@ | ||
module Client.App | ||
|
||
open Fable.Core | ||
open Fable.Core.JsInterop | ||
|
||
open Fable.Import | ||
open Fable.PowerPack | ||
open Elmish | ||
open Elmish.React | ||
open Fable.Import.Browser | ||
open Elmish.Browser.Navigation | ||
open Elmish.HMR | ||
open Client.Shared | ||
open Client.Pages | ||
open ServerCode.Domain | ||
|
||
JsInterop.importSideEffects "whatwg-fetch" | ||
JsInterop.importSideEffects "babel-polyfill" | ||
|
||
/// The composed model for the different possible page states of the application | ||
type PageModel = | ||
| HomePageModel | ||
| LoginModel of Login.Model | ||
| WishListModel of WishList.Model | ||
|
||
/// The composed model for the application, which is a single page state plus login information | ||
type Model = | ||
{ User : UserData option | ||
PageModel : PageModel } | ||
|
||
/// The composed set of messages that update the state of the application | ||
type Msg = | ||
| LoggedIn of UserData | ||
| LoggedOut | ||
| StorageFailure of exn | ||
| LoginMsg of Login.Msg | ||
| WishListMsg of WishList.Msg | ||
| Logout of unit | ||
|
||
/// The navigation logic of the application given a page identity parsed from the .../#info | ||
let handleNotFound (model: Model) = | ||
Browser.console.error("Error parsing url: " + Browser.window.location.href) | ||
( model, Navigation.modifyUrl (toPath Page.Home) ) | ||
|
||
/// The navigation logic of the application given a page identity parsed from the .../#info | ||
/// information in the URL. | ||
let urlUpdate (result:Page option) model = | ||
let urlUpdate (result:Page option) (model: Model) = | ||
match result with | ||
| None -> | ||
Browser.console.error("Error parsing url: " + Browser.window.location.href) | ||
( model, Navigation.modifyUrl (toHash Page.Home) ) | ||
handleNotFound model | ||
|
||
| Some Page.Login -> | ||
let m, cmd = Login.init model.User | ||
|
@@ -69,11 +53,17 @@ let deleteUserCmd = | |
|
||
let init result = | ||
let user = loadUser () | ||
let model = | ||
{ User = user | ||
PageModel = HomePageModel } | ||
|
||
urlUpdate result model | ||
let stateJson: string option = !!Browser.window?__INIT_MODEL__ | ||
match stateJson, result with | ||
| Some json, Some Page.Home -> | ||
let model: Model = ofJson json | ||
{ model with User = user }, Cmd.none | ||
| _ -> | ||
let model = | ||
{ User = user | ||
PageModel = HomePageModel } | ||
|
||
urlUpdate result model | ||
|
||
let update msg model = | ||
match msg, model.PageModel with | ||
|
@@ -89,7 +79,7 @@ let update msg model = | |
| Login.ExternalMsg.NoOp -> | ||
Cmd.none | ||
| Login.ExternalMsg.UserLoggedIn newUser -> | ||
Cmd.ofMsg (LoggedIn newUser) | ||
saveUserCmd newUser | ||
|
||
{ model with | ||
PageModel = LoginModel m }, | ||
|
@@ -104,53 +94,33 @@ let update msg model = | |
{ model with | ||
PageModel = WishListModel m }, Cmd.map WishListMsg cmd | ||
|
||
| WishListMsg _, _ -> | ||
| WishListMsg _, _ -> | ||
model, Cmd.none | ||
|
||
| LoggedIn newUser, _ -> | ||
let nextPage = Page.WishList | ||
{ model with User = Some newUser }, | ||
Cmd.batch [ | ||
saveUserCmd newUser | ||
Navigation.newUrl (toHash nextPage) ] | ||
Navigation.newUrl (toPath nextPage) ] | ||
|
||
| LoggedOut, _ -> | ||
{ model with | ||
User = None | ||
PageModel = HomePageModel }, | ||
Navigation.newUrl (toHash Page.Home) | ||
PageModel = HomePageModel }, | ||
Cmd.batch [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't need to batch here |
||
Navigation.newUrl (toPath Page.Home) ] | ||
|
||
| Logout(), _ -> | ||
model, deleteUserCmd | ||
|
||
// VIEW | ||
|
||
open Fable.Helpers.React | ||
open Fable.Helpers.React.Props | ||
open Client.Style | ||
|
||
/// Constructs the view for a page given the model and dispatcher. | ||
let viewPage model dispatch = | ||
match model.PageModel with | ||
| HomePageModel -> | ||
Home.view () | ||
|
||
| LoginModel m -> | ||
[ Login.view m (LoginMsg >> dispatch) ] | ||
|
||
| WishListModel m -> | ||
[ WishList.view m (WishListMsg >> dispatch) ] | ||
open Elmish.Debug | ||
|
||
/// Constructs the view for the application given the model. | ||
let view model dispatch = | ||
div [] [ | ||
Menu.view (Logout >> dispatch) model.User | ||
hr [] | ||
div [ centerStyle "column" ] (viewPage model dispatch) | ||
] | ||
let withReact = | ||
if (!!Browser.window?__INIT_MODEL__) | ||
then Program.withReactHydrate | ||
else Program.withReact | ||
|
||
open Elmish.React | ||
open Elmish.Debug | ||
|
||
// App | ||
Program.mkProgram init update view | ||
|
@@ -159,7 +129,7 @@ Program.mkProgram init update view | |
|> Program.withConsoleTrace | ||
|> Program.withHMR | ||
#endif | ||
|> Program.withReact "elmish-app" | ||
|> withReact "elmish-app" | ||
#if DEBUG | ||
|> Program.withDebugger | ||
#endif | ||
|
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
File renamed without changes
File renamed without changes
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.
why do we need this? what is it doing?
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.
This will set a compile directive
DEBUG_SSR
to use webpack-dev-server's bundle file path in the server-rendered path. Ideally, inRunSSR
we can trigger client and server to rebuild when we edit any file, so we can dev and debug with SSR; but in normalRun
we would only trigger the server to rebuild when we edit back-end files, and we can dev with webpack-dev-server and use the server code as an API service.But I cannot pass the
configuration
to dotnet-watch to make it work differently in different command, currently, I totally ignored client files' change whenwatch run
the server...https://github.com/SAFE-Stack/SAFE-BookStore/pull/306/files#diff-694c6c636cd70bcc23aa716434744995R12
https://github.com/SAFE-Stack/SAFE-BookStore/pull/306/files#diff-3b3c3fd8d4441c71716037ce14cb42b0R14