Skip to content

Commit c3c7bb2

Browse files
authored
Optimize about page loading (#1464)
Use webpack markdown-loader to compile the about page contents from the README.md file. Remove separate Privacy page, and instead link to file in GitHub repository.
1 parent 3167a00 commit c3c7bb2

32 files changed

+195
-81
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ cabal.sandbox.config
4949
/public/js/generate/app.js
5050
/public/js/graph/app.js
5151
/public/js/draw/app.js
52+
/public/js/about/app.js
5253
/public/*.svg
5354
/public/*.png
5455
/public/*.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
- Refactored the HTTPS response generation from `App/Database/CourseQueries` to the controller `App/Controllers/`
2929
- Moved config values from .hs file to .yaml file
3030
- Changed representation from translation to matrix in `App/Svg/Parser`
31+
- Optimized about page using Webpack and new dependencies `html-loader` and `markdown-loader`
32+
- Removed `privacy` route and codes related to it
3133

3234
## [0.6.0] - 2024-06-24
3335

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ Zi Kai Xu
125125

126126
## Privacy Policy and Licensing
127127

128-
Click [here](/PRIVACY.md) to learn about our Privacy Policy.
128+
Click [here](https://github.com/Courseography/courseography/blob/master/PRIVACY.md) to learn about our Privacy Policy.
129129

130-
Click [here](/LICENSE) to learn about our licensing.
130+
Click [here](https://github.com/Courseography/courseography/blob/master/LICENSE) to learn about our licensing.

app/Config.hs

-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module Config (
1010
serverConf,
1111
databasePath,
1212
runDb,
13-
markdownPath,
1413
graphPath,
1514
genCssPath,
1615
timetableUrl,
@@ -46,7 +45,6 @@ data Config = Config
4645
{ portValue :: Int
4746
, logMessage :: String
4847
, databasePathValue :: Text
49-
, markdownPathValue :: String
5048
, graphPathValue :: String
5149
, genCssPathValue :: String
5250
, timetableUrlValue :: String
@@ -66,7 +64,6 @@ instance FromJSON Config where
6664
<$> obj .: "port"
6765
<*> obj .: "logMessage"
6866
<*> obj .: "databasePath"
69-
<*> obj .: "markdownPath"
7067
<*> obj .: "graphPath"
7168
<*> obj .: "genCssPath"
7269
<*> obj .: "timetableUrl"
@@ -124,10 +121,6 @@ runDb action = do
124121

125122
-- FILE PATH STRINGS
126123

127-
-- | The relative path to the directory with the markdown files rendered for site content.
128-
markdownPath :: IO String
129-
markdownPath = markdownPathValue <$> loadConfig
130-
131124
-- | The relative path to the directory that contains all of the graph SVG files.
132125
graphPath :: IO String
133126
graphPath = graphPathValue <$> loadConfig

app/Response.hs

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ import Response.Image as X
66
import Response.Loading as X
77
import Response.NotFound as X
88
import Response.Post as X
9-
import Response.Privacy as X
109
import Response.Search as X

app/Response/About.hs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
module Response.About
22
(aboutResponse) where
33

4-
import Data.Text.Lazy (Text)
54
import Happstack.Server
65
import MasterTemplate
76
import Text.Blaze ((!))
87
import qualified Text.Blaze.Html5 as H
98
import qualified Text.Blaze.Html5.Attributes as A
10-
import Util.Blaze (mdToHTML)
9+
import Scripts (aboutScripts)
1110

12-
aboutResponse :: Text -> ServerPart Response
13-
aboutResponse aboutContents =
11+
aboutResponse :: ServerPart Response
12+
aboutResponse =
1413
ok $ toResponse $
1514
masterTemplate "Courseography - About"
1615
[]
1716
(do
1817
header "about"
19-
aboutHtml aboutContents
18+
H.div ! A.id "aboutDiv" $ ""
2019
)
21-
""
22-
23-
-- | AboutHtml takes in the contents of the README.md file (the GitHub README file) and translates
24-
-- the markdown to blaze-HTML.
25-
aboutHtml :: Text -> H.Html
26-
aboutHtml contents = H.div ! A.id "aboutDiv" $ mdToHTML contents
20+
aboutScripts

app/Response/Privacy.hs

-26
This file was deleted.

app/Routes.hs

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Controllers.Graph as GraphsController
77
( graphResponse, index, getGraphJSON, graphImageResponse )
88
import Controllers.Generate as GenerateController (generateResponse, findAndSavePrereqsResponse)
99
import Controllers.Timetable as TimetableController
10-
import Data.Text.Lazy (Text)
1110
import Database.CourseInsertion (saveGraphJSON)
1211
import Database.CourseQueries (retrievePost)
1312
import Happstack.Server
@@ -24,21 +23,20 @@ import Happstack.Server
2423
import Response
2524
( drawResponse,
2625
aboutResponse,
27-
privacyResponse,
2826
notFoundResponse,
2927
searchResponse,
3028
postResponse,
3129
loadingResponse)
3230

33-
routeResponses :: String -> Text -> Text -> ServerPartT IO Response
34-
routeResponses staticDir aboutContents privacyContents =
35-
msum (map strictMatchDir (strictRoutes aboutContents privacyContents) ++
31+
routeResponses :: String -> ServerPartT IO Response
32+
routeResponses staticDir =
33+
msum (map strictMatchDir strictRoutes ++
3634
[dir "static" $ serveDirectory DisableBrowsing [] staticDir,
3735
nullDir >> seeOther ("graph" :: String) (toResponse ("Redirecting to /graph" :: String)),
3836
notFoundResponse])
3937

40-
strictRoutes :: Text -> Text -> [ (String, ServerPart Response)]
41-
strictRoutes aboutContents privacyContents = [
38+
strictRoutes :: [(String, ServerPart Response)]
39+
strictRoutes = [
4240
("grid", TimetableController.gridResponse),
4341
("graph", GraphsController.graphResponse),
4442
("graph-generate", GenerateController.findAndSavePrereqsResponse),
@@ -48,8 +46,7 @@ strictRoutes aboutContents privacyContents = [
4846
("post", retrievePost),
4947
("post-progress", postResponse),
5048
("draw", drawResponse),
51-
("about", aboutResponse aboutContents),
52-
("privacy", privacyResponse privacyContents),
49+
("about", aboutResponse),
5350
("graphs", GraphsController.index),
5451
("timesearch", searchResponse),
5552
("generate", generateResponse),

app/Scripts.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Scripts (
2-
graphScripts, timetableScripts, drawScripts, postScripts, searchScripts, generateScripts
2+
graphScripts, timetableScripts, drawScripts, postScripts, searchScripts, generateScripts, aboutScripts,
33
)
44
where
55

@@ -35,3 +35,7 @@ searchScripts = do
3535
generateScripts :: H.Html
3636
generateScripts = do
3737
H.script ! A.src "/static/js/generate/app.js" $ ""
38+
39+
aboutScripts :: H.Html
40+
aboutScripts = do
41+
H.script ! A.src "/static/js/about/app.js" $ ""

app/Server.hs

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ responses.
99
module Server
1010
(runServer) where
1111

12-
import Config (markdownPath, serverConf)
12+
import Config (serverConf)
1313
import Control.Concurrent (forkIO, killThread)
1414
import Data.String (fromString)
15-
import qualified Data.Text.Lazy.IO as LazyIO
1615
import Filesystem.Path.CurrentOS as Path
1716
import Happstack.Server hiding (host)
1817
import Routes (routeResponses)
@@ -25,15 +24,12 @@ runServer :: IO ()
2524
runServer = do
2625
configureLogger
2726
staticDir <- getStaticDir
28-
markdown <- markdownPath
29-
aboutContents <- LazyIO.readFile $ markdown ++ "README.md"
30-
privacyContents <- LazyIO.readFile $ markdown ++ "PRIVACY.md"
3127

3228
-- Start the HTTP server
3329
server <- serverConf
3430
httpThreadId <- forkIO $ simpleHTTP server $ do
3531
decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096)
36-
routeResponses staticDir aboutContents privacyContents
32+
routeResponses staticDir
3733
waitForTermination
3834
killThread httpThreadId
3935
where

config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ logMessage: "Happstack.Server.AccessLog.Combined"
66
databasePath: "db/database.sqlite3"
77

88
# File Path Strings
9-
markdownPath: "./"
109
graphPath: "./graphs/"
1110
genCssPath: "./public/style/"
1211

courseography.cabal

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ executable courseography
9494
Response.Loading,
9595
Response.NotFound,
9696
Response.Post,
97-
Response.Privacy,
9897
Response.Search,
9998
Routes,
10099
Scripts,

js/components/about/about.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import aboutContent from "../../../README.md"
2+
3+
document.addEventListener("DOMContentLoaded", () => {
4+
const container = document.getElementById("aboutDiv")
5+
container.innerHTML = aboutContent
6+
})

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@
5353
"eslint-plugin-prettier": "5",
5454
"eslint-plugin-react": "^7.34.1",
5555
"fetch-mock": "^9.11.0",
56+
"html-loader": "^5.1.0",
5657
"husky": "^9.0.11",
5758
"jest": "^29.7.0",
5859
"jest-environment-jsdom": "^29.7.0",
60+
"markdown-loader": "^8.0.0",
5961
"mini-css-extract-plugin": "^2.7.6",
6062
"node-fetch": "^3.3.2",
6163
"postcss": "^8.4.38",

style/app.scss

-13
Original file line numberDiff line numberDiff line change
@@ -1943,19 +1943,6 @@ div[id*="_inq"] .code::after {
19431943
color: #437699;
19441944
}
19451945

1946-
#privacyDiv {
1947-
margin: 0 auto;
1948-
max-width: 1000px;
1949-
padding: 0 1em;
1950-
text-align: justify;
1951-
}
1952-
1953-
#privacyDiv > h1,
1954-
#privacyDiv > h2,
1955-
#privacyDiv > h3 {
1956-
color: #437699;
1957-
}
1958-
19591946
#contentDiv {
19601947
margin: 0 25px;
19611948
}

webpack.common.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
"js/post/app": "./js/components/post/post.js.jsx",
1919
"js/draw/app": "./js/components/draw/main.js",
2020
"js/generate/app": "./js/components/generate/generate.jsx",
21+
"js/about/app": "./js/components/about/about.js",
2122
"style/app": "./style/app.js",
2223
},
2324
output: {
@@ -51,6 +52,10 @@ module.exports = {
5152
test: /\.s[ac]ss$/i,
5253
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
5354
},
55+
{
56+
test: /\.md$/,
57+
use: [{ loader: "html-loader" }, { loader: "markdown-loader" }],
58+
},
5459
],
5560
},
5661
plugins: [

0 commit comments

Comments
 (0)