Improved the static assets definitions in the Website service (#18)
This PR contains the work done to overhaul the static asset definitions for the Website service.
To provide further details about the work:
* Website library
* Overhauled the `StateFile` enumeration to reduce the number of cases to one case per logical file name, each exposing a `fileExtensions` list.
* Wired everything into the pages with a consistent ordering convention: stylesheets load shared-first so the page sheet wins the CSS cascade; scripts load page-first with shared.js last. The error page also gained the shared stylesheet and its scripts; the index page gained its page CSS/JS and the new touch icon link.
* Used the `StaticFile` enumeration as a single source of truth for every _href_/_src_ in the `IndexPage` and the `ErrorPage` pages, eliminating hardcoded asset paths.
* Website service
* Added new assets to the Resources folder:
* `apple-touch-icon.png`
* `css/index.css`
* `js/index.js`
* `js/error.js`
* `sitemap.xml`
* Renamed existing assets within the Resources folder:
* `css/style.css` → `css/shared.css`
* `js/app.js` → `js/shared.js`
* Fixed the working-directory location for the scheme in the Xcode project.
Reviewed-on: rock-n-code/loud-amsterdam#18
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
@@ -1,24 +1,27 @@
|
||||
/// A static file shipped with the website service.
|
||||
///
|
||||
/// Each case identifies a file stored under the static files root (the `Resources/Static`
|
||||
/// directory) and served by Hummingbird's `FileMiddleware` middleware.
|
||||
/// Each case identifies a file name stored under the static files root (the `Resources/Static`
|
||||
/// directory) and served by Hummingbird's `FileMiddleware` middleware. A name can be available
|
||||
/// with more than one extension (see ``fileExtensions``), each resolving to its own file.
|
||||
enum StaticFile: CaseIterable, Sendable {
|
||||
/// The `js/app.js` script.
|
||||
case appJS
|
||||
/// The `css/error.css` stylesheet for the not-found page.
|
||||
case errorCSS
|
||||
/// The `apple-touch-icon.png` icon.
|
||||
case appleTouchIcon
|
||||
/// The `css/error.css` stylesheet and `js/error.js` script for the not-found page.
|
||||
case error
|
||||
/// The `favicon.ico` icon.
|
||||
case faviconICO
|
||||
/// The `icon.png` icon.
|
||||
case iconPNG
|
||||
/// The `icon.svg` icon.
|
||||
case iconSVG
|
||||
case favicon
|
||||
/// The `icon.png` and `icon.svg` icons.
|
||||
case icon
|
||||
/// The `css/index.css` stylesheet and `js/index.js` script for the landing page.
|
||||
case index
|
||||
/// The `robots.txt` crawler directives.
|
||||
case robotsTXT
|
||||
case robots
|
||||
/// The `css/shared.css` stylesheet and `js/shared.js` script shared across pages.
|
||||
case shared
|
||||
/// The `site.webmanifest` web application manifest.
|
||||
case siteWebmanifest
|
||||
/// The `css/style.css` stylesheet.
|
||||
case styleCSS
|
||||
case site
|
||||
/// The `sitemap.xml` crawler sitemap.
|
||||
case sitemap
|
||||
}
|
||||
|
||||
// MARK: - Enumerations
|
||||
@@ -40,6 +43,8 @@ extension StaticFile {
|
||||
case txt
|
||||
/// A web application manifest file.
|
||||
case webmanifest
|
||||
/// An Extensible Markup Language file.
|
||||
case xml
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,9 +54,91 @@ extension StaticFile {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The file extensions the file is available with.
|
||||
var fileExtensions: [Extension] {
|
||||
switch self {
|
||||
case .appleTouchIcon: [.png]
|
||||
case .error,
|
||||
.index,
|
||||
.shared: [.css, .js]
|
||||
case .favicon: [.ico]
|
||||
case .icon: [.png, .svg]
|
||||
case .robots: [.txt]
|
||||
case .site: [.webmanifest]
|
||||
case .sitemap: [.xml]
|
||||
}
|
||||
}
|
||||
|
||||
/// The file's name, without extension.
|
||||
var fileName: String {
|
||||
switch self {
|
||||
case .appleTouchIcon: "apple-touch-icon"
|
||||
case .error: "error"
|
||||
case .favicon: "favicon"
|
||||
case .icon: "icon"
|
||||
case .index: "index"
|
||||
case .robots: "robots"
|
||||
case .shared: "shared"
|
||||
case .site: "site"
|
||||
case .sitemap: "sitemap"
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// Resolves the file's path against the given base directory.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - basePath: the directory the static files are served from.
|
||||
/// - fileExtension: the extension of the file to resolve.
|
||||
/// - Returns: the path to the file, relative to the `basePath` path.
|
||||
func path(
|
||||
relativeTo basePath: String,
|
||||
for fileExtension: Extension
|
||||
) -> String {
|
||||
let relativePath = relativePath(for: fileExtension)
|
||||
|
||||
guard !basePath.isEmpty else {
|
||||
return relativePath
|
||||
}
|
||||
|
||||
return "\(basePath)/\(relativePath)"
|
||||
}
|
||||
|
||||
/// Resolves the file's path relative to the static files root (e.g. `"css/shared.css"`).
|
||||
///
|
||||
/// This also matches the URL path the file is served at by `FileMiddleware`.
|
||||
///
|
||||
/// - Parameter fileExtension: the extension of the file to resolve.
|
||||
/// - Returns: the path to the file, relative to the static files root.
|
||||
func relativePath(
|
||||
for fileExtension: Extension
|
||||
) -> String {
|
||||
let file = "\(fileName).\(fileExtension.rawValue)"
|
||||
|
||||
return fileExtension.subdirectory
|
||||
.map { "\($0)/\(file)" } ?? file
|
||||
}
|
||||
|
||||
/// Resolves the absolute URL path the file is served at (e.g. `"/css/shared.css"`).
|
||||
///
|
||||
/// - Parameter fileExtension: the extension of the file to resolve.
|
||||
/// - Returns: the path to use in `href` and `src` attributes.
|
||||
func urlPath(
|
||||
for fileExtension: Extension
|
||||
) -> String {
|
||||
"/\(relativePath(for: fileExtension))"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension StaticFile.Extension {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The file's content type.
|
||||
var contentType: String {
|
||||
switch fileExtension {
|
||||
switch self {
|
||||
case .css: "text/css"
|
||||
case .js: "text/javascript"
|
||||
case .png: "image/png"
|
||||
@@ -59,77 +146,15 @@ extension StaticFile {
|
||||
case .svg: "image/svg+xml"
|
||||
case .txt: "text/plain"
|
||||
case .webmanifest: "application/manifest+json"
|
||||
case .xml: "application/xml"
|
||||
}
|
||||
}
|
||||
|
||||
/// The file's extension.
|
||||
var fileExtension: Extension {
|
||||
switch self {
|
||||
case .errorCSS,
|
||||
.styleCSS: .css
|
||||
case .appJS: .js
|
||||
case .faviconICO: .ico
|
||||
case .iconPNG: .png
|
||||
case .iconSVG: .svg
|
||||
case .robotsTXT: .txt
|
||||
case .siteWebmanifest: .webmanifest
|
||||
}
|
||||
}
|
||||
|
||||
/// The file's name, without extension.
|
||||
var fileName: String {
|
||||
switch self {
|
||||
case .appJS: "app"
|
||||
case .errorCSS: "error"
|
||||
case .faviconICO: "favicon"
|
||||
case .iconPNG,
|
||||
.iconSVG: "icon"
|
||||
case .robotsTXT: "robots"
|
||||
case .siteWebmanifest: "site"
|
||||
case .styleCSS: "style"
|
||||
}
|
||||
}
|
||||
|
||||
/// The path relative to the static files root (e.g. `"css/style.css"`).
|
||||
///
|
||||
/// This also matches the URL path the file is served at by `FileMiddleware`.
|
||||
var relativePath: String {
|
||||
let file = "\(fileName).\(fileExtension.rawValue)"
|
||||
|
||||
return subdirectory
|
||||
.map { "\($0)/\(file)" } ?? file
|
||||
}
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// Resolves the file's path against the given base directory.
|
||||
///
|
||||
/// - Parameter basePath: the directory the static files are served from.
|
||||
/// - Returns: the path to the file, relative to the `basePath` path.
|
||||
func path(
|
||||
relativeTo basePath: String
|
||||
) -> String {
|
||||
guard !basePath.isEmpty else {
|
||||
return relativePath
|
||||
}
|
||||
|
||||
return "\(basePath)/\(relativePath)"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension StaticFile {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The sub-directory within the static root that holds the file, if any.
|
||||
/// The sub-directory within the static root that holds files with this extension, if any.
|
||||
var subdirectory: String? {
|
||||
switch self {
|
||||
case .appJS: "js"
|
||||
case .errorCSS,
|
||||
.styleCSS: "css"
|
||||
case .css: "css"
|
||||
case .js: "js"
|
||||
default: nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user