Skip to content

Commit

Permalink
Server side routing works 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasJayanth committed Jul 28, 2024
1 parent c803c88 commit be1ef5c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
38 changes: 31 additions & 7 deletions bin/main.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[@alert "-all--all+deprecated"];
module Page = {
[@react.component]
let make = (~scripts=[], ~appHTMLString) => {
Expand Down Expand Up @@ -29,21 +30,44 @@ module Page = {
};
};

let hello_route_handler = _request => {
let renderPage = reactElement => {
let appHTMLString =
ReactDOM.renderToString(<Reason_india_website_native.App />);
ReactDOM.renderToString(reactElement);
ReactDOM.renderToString(
<Page scripts=["/static/app.js"] appHTMLString />,
);
}




let hello_route_handler = request => {
let target = request
|> Dream.path
|> List.filter(p => !String.equal("", p))
|> String.concat("/");

print_endline("---------------");
print_endline(target);

let reactElement =
switch (Routes.match'(Reason_india_website_native.AppRouter.routes, ~target)) {
| Routes.FullMatch(e) => e()
| Routes.MatchWithTrailingSlash(e) => e()
| Routes.NoMatch => <p> {React.string("404")} </p>
};

Dream.html(
ReactDOM.renderToString(
<Page scripts=["/static/app.js"] appHTMLString />,
),
renderPage(reactElement)
);
};

let hello_route: Dream.route = Dream.get("/", hello_route_handler);
let statics_route: Dream.route =
Dream.get("/static/**", Dream.static("./static"));

let handler = Dream.router([hello_route, statics_route]);
let hello_route: Dream.route = Dream.get("/**", hello_route_handler);

let handler = Dream.router([statics_route, hello_route]);

// request => promise(response)
Dream.run(~port=8000, ~interface="localhost", handler);
19 changes: 2 additions & 17 deletions client/app.re
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
let home = () => Routes.(nil); // '/'
let hello = () => Routes.(s("hello") /? nil); // /hello

let homeRoute = Routes.(home() @--> (() => {
print_endline("in home page");
<Reason_india_website_js.App />;
}));

let helloRoute = Routes.(hello() @--> (() => {
print_endline("in hello view");
<p> {React.string("In Hello")} </p>
}));

let routes = Routes.one_of([homeRoute, helloRoute]);

module Main = {
[@react.component]
let make = () => {
let target =
ReasonReactRouter.useUrl().path
|> String.concat("/");
switch (Routes.match'(routes, ~target)) {
|> String.concat("/");
switch (Routes.match'(Reason_india_website_js.AppRouter.routes, ~target)) {
| Routes.FullMatch(e) => e()
| Routes.MatchWithTrailingSlash(e) => e()
| Routes.NoMatch => <p> {React.string("404")} </p>
Expand Down
2 changes: 1 addition & 1 deletion lib/js/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(library
(name reason_india_website_js)
(modes melange)
(libraries styled-ppx.css styled-ppx.emotion melange reason-react)
(libraries routes styled-ppx.css styled-ppx.emotion melange reason-react)
(preprocess
(pps
server-reason-react.browser_ppx -js
Expand Down
14 changes: 14 additions & 0 deletions lib/native/AppRouter.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let home = () => Routes.(nil); // '/'
let hello = () => Routes.(s("hello") /? nil); // /hello

let homeRoute = Routes.(home() @--> (() => {
print_endline("in home page");
<App />;
}));

let helloRoute = Routes.(hello() @--> (() => {
print_endline("in hello view");
<p> {React.string("In Hello")} </p>
}));

let routes = Routes.one_of([homeRoute, helloRoute]);
5 changes: 3 additions & 2 deletions lib/native/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
(libraries
styled-ppx.emotion_native
styled-ppx.css_native
routes
server-reason-react.react
server-reason-react.reactDom)
server-reason-react.reactDom
routes
)
(preprocess
(pps styled-ppx --native
server-reason-react.browser_ppx
Expand Down

0 comments on commit be1ef5c

Please sign in to comment.