簡體   English   中英

托管 Django 並與 Heroku 上的包裹捆綁器反應

[英]Hosting Django And React with parcel bundler on Heroku

我一直在嘗試使用 Parcel 捆綁器在前端使用 react 托管我的 Django rest 應用程序,成功托管后,控制台中的 ZBC4150D023D3255136 和 js 類型錯誤 DB671D61AC93F2Z 和 js 未加載

拒絕應用來自 'https://minglemarket.herokuapp.com/src.aaba6b6a.css' 的樣式,因為它的 MIME 類型 ('text/html') 不是受支持的樣式表 MIME 類型,並且啟用了嚴格的 MIME 檢查。 minglemarket.herokuapp.com/:1 拒絕應用來自“https://minglemarket.herokuapp.com/style.e1ff692e.css”的樣式,因為它的 MIME 類型(“text/html”)不是受支持的樣式表 MIME 類型,並且啟用嚴格的 MIME 檢查。 minglemarket.herokuapp.com/:1 拒絕執行來自 'https://minglemarket.herokuapp.com/src.8d3ada04.js' 的腳本,因為它的 MIME 類型('text/html')不可執行,並且嚴格的 MIME 類型檢查已啟用。

我在 Heroku 上的 Django/React/Django Restframework/Webpack 站點上遇到了類似的問題。 我得到了同樣的錯誤。 實際上,我不得不不止一次地修復它,並且我用兩種不同的方式修復了它。 解決 static 文件的 MIME 類型問題的一種方法是設置我的前端 urls.py 文件,如下所示:

from django.urls import path, re_path
from django.views.generic import TemplateView

urlpatterns = [
    re_path(r'^robots\.txt$', TemplateView.as_view(
        template_name="frontend/robots.txt", content_type='text/plain')),
    re_path(r'^manifest\.json$', TemplateView.as_view(
        template_name="frontend/manifest.json", content_type='application/json')),
    re_path(r'^.*', TemplateView.as_view(
        template_name='frontend/index.html',
        content_type='text/html')),

我能夠解決此問題的另一種方法是確保我在 Webpack 中設置了 publicPath,我不確定它如何與 Parcel 一起使用。 我在 webpack 中將它設置為 publicPath: '/',但它主要只是需要設置一些東西。

我使用過 Parcel,但最終它提供的包裹比 webpack 大得多,所以我切換回 webpack。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM