Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #382 from communitiesuk/FSPT-156-fix-serving-stati…
Browse files Browse the repository at this point in the history
…c-assets-left

FSPT-156 Fix serving static fonts and favicon
  • Loading branch information
sfount authored Dec 11, 2024
2 parents d3dcb8d + b13bf97 commit 72209c2
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 36 deletions.
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def create_app() -> Flask:
"Authenticator",
specification_dir="/openapi/",
options=connexion_options,
server_args={"static_url_path": "/assets"},
)
connexion_app.add_api(
get_bundled_specs(Config.FLASK_ROOT + "/openapi/api.yml"),
Expand All @@ -61,7 +62,7 @@ def create_app() -> Flask:
# Configure Flask App
flask_app = connexion_app.app
flask_app.config.from_object("config.Config")
flask_app.static_folder = "frontend/static/dist/"
flask_app.static_folder = Config.STATIC_FOLDER
flask_app.jinja_loader = ChoiceLoader(
[
PackageLoader("frontend"),
Expand Down
30 changes: 2 additions & 28 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import glob
import os
import shutil
import urllib.request
Expand All @@ -9,16 +8,10 @@

def build_govuk_assets(static_dist_root="frontend/static/dist"):

# NOTE: When using connexion for our openapi management
# FLASK_STATIC_URL needs to be "/static"
# as static_url_path is not directly configurable
# with the connexion app constructor connexion.FlaskApp()
# so the default /static url needs to be used
DIST_ROOT = "./" + static_dist_root
GOVUK_DIR = "/govuk-frontend"
GOVUK_URL = "https://github.com/alphagov/govuk-frontend/releases/download/v4.0.0/release-v4.0.0.zip"
GOVUK_URL = "https://github.com/alphagov/govuk-frontend/releases/download/v4.8.0/release-v4.8.0.zip"
ZIP_FILE = "./govuk_frontend.zip"
DIST_PATH = DIST_ROOT + GOVUK_DIR
DIST_PATH = DIST_ROOT
ASSETS_DIR = "/assets"
ASSETS_PATH = DIST_PATH + ASSETS_DIR

Expand Down Expand Up @@ -56,25 +49,6 @@ def build_govuk_assets(static_dist_root="frontend/static/dist"):
for file_to_move in os.listdir(ASSETS_PATH):
shutil.move("/".join([ASSETS_PATH, file_to_move]), DIST_PATH)

# Update relative paths

print("Updating relative paths in css files to " + GOVUK_DIR)
cwd = os.getcwd()
os.chdir(DIST_PATH)
for css_file in glob.glob("*.css"):

# Read in the file
with open(css_file, "r") as file:
filedata = file.read()

# Replace the target string
filedata = filedata.replace(ASSETS_DIR, ASSETS_DIR + GOVUK_DIR)

# Write the file out again
with open(css_file, "w") as file:
file.write(filedata)
os.chdir(cwd)

# Delete temp files
print("Deleting " + ASSETS_PATH)
shutil.rmtree(ASSETS_PATH)
Expand Down
2 changes: 1 addition & 1 deletion frontend/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

{% block bodyEnd %}
<!--[if gt IE 8]><!-->
<script nonce="{{ csp_nonce() }}" src="{{ url_for('static', filename='govuk-frontend/govuk-frontend-4.0.0.min.js') }}"> </script>
<script nonce="{{ csp_nonce() }}" src="{{ url_for('static', filename='govuk-frontend-4.8.0.min.js') }}"> </script>
<script nonce="{{ csp_nonce() }}">

window.addEventListener('load', (event) => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/templates/head.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<meta name="description" content="{{ base_title }}">
<meta name="keywords" content="{{ base_title }}">
<meta name="author" content="{{ service_meta_author }}">
<!--[if gt IE 8]><!--><link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='govuk-frontend/govuk-frontend-4.0.0.min.css') }}" /><!--<![endif]-->
<!--[if gt IE 8]><!--><link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='govuk-frontend-4.8.0.min.css') }}" /><!--<![endif]-->
<!--[if IE 8]><link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='govuk-frontend/govuk-frontend-ie8-4.0.0.min.css') }}" /><![endif]-->
{% assets "main_js" %}
<script nonce="{{ csp_nonce() }}" type="text/javascript" src="{{ ASSET_URL }}" defer></script>
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
"flask-wtf==1.2.2",
"flask==2.2.5",
"funding-service-design-utils==5.2.0",
"govuk-frontend-jinja==2.3.0",
"govuk-frontend-jinja==2.8.0",
"greenlet==3.1.1",
"jsmin==3.0.1",
"msal==1.28.0",
Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72209c2

Please sign in to comment.