简体   繁体   English

无法读取使用 Node.js 上传到 S3 存储桶的 css 文件

[英]Unable to read css file uploaded to S3 bucket with Node.js

I have a Node.js app that is meant to upload a css file to S3 to be used by my website.我有一个 Node.js 应用程序,用于将 css 文件上传到 S3 以供我的网站使用。 Everything seems to work fine but when I access the file on my website none of the css changes are being applied.一切似乎都运行良好,但是当我访问我网站上的文件时,没有应用任何 css 更改。 I can even see the css file under 'dev tools/sources'.我什至可以在“开发工具/源”下看到 css 文件。 The css in that file though is not taking effect.该文件中的 css 虽然没有生效。 If I make any change to the file in dev tools the css starts to immediately all work.如果我在开发工具中对文件进行任何更改,css 将立即开始所有工作。 If I download the file from s3 and then reupload it manually without changing anything that also works.如果我从 s3 下载文件然后手动重新上传它而不更改任何也有效的文件。 So something to do with the formatting of me adding the file with Node.js is throwing it off.因此,与我添加带有 Node.js 的文件的格式有关的事情正在被抛弃。 Any help with this would be greatly appreciated.对此的任何帮助将不胜感激。

 let globalStyles = `.header-background{background-color:${themeStyles['headerBackground']};} //using this to remove backticks from globalStyles globalStyles = globalStyles.replace(/^`|`$/g, ''); await addFileCssS3(css, `${newUrl}/global.css`, newUrl); const addFileCssS3 = async (file, key, newUrl) => { await s3.putObject({ Body: file, Bucket: BucketName, Key: key, ContentType: 'text/css', }).promise().catch((error) => { console.error(error) }) }

To fix this, you can try removing the newline from the globalStyles string before passing it to the addFileCssS3 method.要解决此问题,您可以尝试在将换行符传递给addFileCssS3方法之前从globalStyles字符串中删除换行符。 There are several ways to do this, one is to simply use the .trim() property to remove any leading and trailing whitespace from a string.有几种方法可以做到这一点,一种是简单地使用.trim()属性从字符串中删除任何前导和尾随空格。

For example:例如:

globalStyles = globalStyles.trim();

Or, by using a regular expression to remove the newline character from the end of the globalStyles string:或者,通过使用正则表达式从globalStyles字符串的末尾删除换行符:

globalStyles = globalStyles.replace(/\r?\n|\r/g, '');

The second method, using regular expression, should work to remove newlines from both Windows and Linux.第二种方法使用正则表达式,应该可以从 Windows 和 Linux 中删除换行符。

Another possible cause could be the failed call to a function that re-inserts the CSS into the html page, the eventual lack of this function causes the downloaded CSS not to be used.另一个可能的原因可能是对 function 的调用失败,将 CSS 重新插入到 html 页面,最终缺少此 function 导致下载的 CSS 无法使用。

If this doesn't solve the problem, I recommend checking the Content-Type header of the CSS file when uploaded to S3 to make sure it's set correctly.如果这不能解决问题,我建议在上传到 S3 时检查 CSS 文件的 Content-Type header 以确保其设置正确。 Also check if the S3 bucket permissions or browser caching parameters are configured to not allow the use of the updated CSS.还要检查 S3 存储桶权限或浏览器缓存参数是否配置为不允许使用更新后的 CSS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何使用 node.js 将 m3u8 文件和相应的 ts 文件上传到 amazon s3 存储桶 - How can we upload m3u8 file and corresponding ts file to amazon s3 bucket using node.js 将大于 1MB 的文件上传到 S3 Bucket (Node.js) 时出现错误代码 500 - Get the error code 500 when I upload a file size > 1MB to S3 Bucket (Node.js) 为上传到 GCP 存储桶的文件提供自定义元数据的正确方法是什么? 使用 node.js sdk - What is the right way of providing custom metadata to file uploaded to GCP Storage bucket? Using node.js sdk 无法使用 boto3 从 s3 存储桶中读取并写入同一存储桶中的不同文件 - Unable to read from s3 bucket and write to same bucket a different file using boto3 用于将文件从 s3 复制到 s3 存储桶的节点 js 代码 - node js code for copying file from s3 to s3 bucket 无法从 node.js 中的 S3 获取文件 - Unable to fetch file from S3 in node js 将最新上传的文件从 S3 存储桶复制到本地机器 - Copy the latest uploaded file from S3 bucket to local machine 使用节点中 aws s3 存储桶的范围读取部分视频文件 - Read partial video file using range from aws s3 bucket in node 将图像上传到 s3 存储桶 - 反应本机和 node.js - Upload image to s3 bucket - react native and node js 使用 boto3 从 S3 存储桶中读取文件内容 - Read file content from S3 bucket with boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM