squidfunk / mkdocs-material

Documentation that simply works

Home Page:https://squidfunk.github.io/mkdocs-material/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Privacy plugin] Content-type error downloading webp images

Guts opened this issue · comments

Contribution guidelines

I've found a bug and checked that ...

  • ... the problem doesn't occur with the mkdocs or readthedocs themes
  • ... the problem persists when all overrides are removed, i.e. custom_dir, extra_javascript and extra_css
  • ... the documentation does not mention anything about my problem
  • ... there are no open or closed issues that are related to my problem

Description

If a markdown page contains a remote webp image, the download processed by the privacy plugin fails.

Expected behaviour

Download webp images as others formats.

Actual behaviour

Here comes the log with mkdocs serve -s -v:

DEBUG    -  Downloading external file:
            https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/qgis/qgis_kore_global.webp
ERROR    -  Error building page 'index.md': 'content-type'
Traceback (most recent call last):
  File "/tmp/test-privacy/.venv/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/mkdocs/__main__.py", line 192, in build_command
    build.build(config.load_config(**kwargs), dirty=not clean)
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/mkdocs/commands/build.py", line 314, in build
    _build_page(file.page, config, doc_files, nav, env, dirty)
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/mkdocs/commands/build.py", line 220, in _build_page
    output = config['plugins'].run_event(
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/mkdocs/plugins.py", line 102, in run_event
    result = method(item, **kwargs)
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/material/plugins/privacy/plugin.py", line 137, in on_post_page
    value.replace(raw, self.__fetch(url, page)),
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/material/plugins/privacy/plugin.py", line 209, in __fetch
    name = re.findall(r'^[^;]+', res.headers["content-type"])[0]
  File "/tmp/test-privacy/.venv/lib/python3.8/site-packages/requests/structures.py", line 54, in __getitem__
    return self._store[key.lower()][1]
KeyError: 'content-type'

Steps to reproduce

Add a markdwon page with a remote webp image:

# Welcome to MkDocs

For full documentation visit [mkdocs.org](https://www.mkdocs.org).

## Commands

* `mkdocs new [dir-name]` - Create a new project.
* `mkdocs serve` - Start the live-reloading docs server.
* `mkdocs build` - Build the documentation site.
* `mkdocs -h` - Print help message and exit.

## Project layout

    mkdocs.yml    # The configuration file.
    docs/
        index.md  # The documentation homepage.
        ...       # Other markdown pages, images and other files.

![test image](https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/qgis/qgis_kore_global.webp)

Package versions

  • Python: 3.8
  • MkDocs: 1.3.0
  • Material: 8.2.16-insiders-4.16.2

Configuration

site_name: Test privacy plugin

theme:
  name: "material"

plugins:
  - privacy:
      enabled: true

System information

Ubuntu LTS

Thanks for reporting. webp images are supported by the privacy plugin. However, your webserver doesn't set the Content-Type header to image/webp, which is required for images to be correctly handled by all browsers. If you set the header, downloading should work as expected.

Bildschirmfoto 2022-05-30 um 14 05 36

For further reference, here are the mime types that are automatically downloaded by the privacy plugin:

extensions = dict({
    "application/javascript": ".js",
    "image/avif": ".avif",
    "image/gif": ".gif",
    "image/jpeg": ".jpg",
    "image/png": ".png",
    "image/svg+xml": ".svg",
    "image/webp": ".webp",
    "text/javascript": ".js",
    "text/css": ".css"
})

Thanks for reporting. webp images are supported by the privacy plugin. However, your webserver doesn't set the Content-Type header to image/webp, which is required for images to be correctly handled by all browsers. If you set the header, downloading should work as expected.

Oh, you're damn right! I realized that the image/webp mime-type was not recognized by the system (in /etc/mime.types) and so by Apache.

Thanks for your input and sorry for the noise.

No worries, glad I could help. We could try to infer the mime type from the file extension, but that'll be another can of worms I don't really want to open. Thus, assuming the correct content types to be served is a reasonable compromise 😊