Skip to content

Commit

Permalink
Serve custom 404 pages during preview
Browse files Browse the repository at this point in the history
  • Loading branch information
norm committed Jun 16, 2021
1 parent b9ed170 commit 2c81ec2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
16 changes: 16 additions & 0 deletions documentation/release-notes.markdown
Expand Up @@ -2,6 +2,22 @@

# Release notes for Flourish

## NEW_VERSION - UNRELEASED

High level description, if applicable.

#### New

* The preview server can now serve your custom 404 page.


#### Other changes

#### Bug fixes

#### Dependency updates


## 0.10.2 - 15 Jun 2021

* Fix a bug in the redirects uploader that stopped upload working
Expand Down
9 changes: 9 additions & 0 deletions documentation/site-configuration.markdown
Expand Up @@ -73,6 +73,15 @@ There are other keys that Flourish will treat as having special meaning.
`301 Moved Permanently` response by the Flourish preview server,
and by S3/CloudFront after the site is uploaded.

* `404_page`

```
404_page = '/not_found'
```

If you are producing a custom 404 page, you can tell Flourish's
preview server to serve it instead of the default 404 error.


## Using in templates

Expand Down
38 changes: 29 additions & 9 deletions flourish/command_line.py
Expand Up @@ -11,6 +11,7 @@
from flask import Flask, make_response, redirect, request, send_from_directory
from jinja2 import Template
from sectile import Sectile
from werkzeug.exceptions import NotFound

from . import Flourish, __version__, blueprint
from .examples import example_files
Expand Down Expand Up @@ -228,6 +229,13 @@ def preview_server(args):
def send_file(path=''):
generate = '/%s' % path

try:
not_found_page = flourish.site_config['404_page']
if not_found_page.startswith('/'):
not_found_page = not_found_page[1:]
except:
not_found_page = None

if generate in flourish.redirects:
return redirect(flourish.redirects[generate])

Expand All @@ -243,25 +251,37 @@ def send_file(path=''):
return response
else:
# fix URLs for .html
filename = os.path.basename(path)
if filename == '':
path = '%sindex.html' % path
_, ext = os.path.splitext(path)
send = path
if '' == os.path.basename(path):
send = send + 'index.html'
_, ext = os.path.splitext(send)
if ext == '':
path += '.html'
send += '.html'

# regenerate if requested
if args.generate:
try:
os.remove(os.path.join(output_dir, path))
os.remove(os.path.join(output_dir, send))
except FileNotFoundError:
pass
flourish.generate_path(generate, report=True)

if os.path.exists(os.path.join(output_dir, path)):
return send_from_directory(output_dir, path)
if os.path.exists(os.path.join(output_dir, send)):
return send_from_directory(output_dir, send)
else:
return send_from_directory(source_dir, path)
if not_found_page:
try:
return send_from_directory(source_dir, send)
except NotFound:
# avoid looping on 404 fetching 404
if path != not_found_page:
response = send_file(not_found_page)
response.status_code = 404
return response
else:
raise NotFound
else:
return send_from_directory(source_dir, send)

@app.route('/_sectile/update', methods=['GET', 'POST'])
def edit_sectile_fragment():
Expand Down

0 comments on commit 2c81ec2

Please sign in to comment.