0

I am using gulp to uglify, concat and compress my javasscripts. When i run all.js script on my website its give me below highlighted error. But if i don't compress and only concat and ugilfy everything works fine than. I don't know why its is giving me this error by compressing. For compressing i am using jstuckey/gulp-gzip module.

Browser console:

Uncaught SyntaxError: Invalid or unexpected token

gulpfile.js:

gulp.task('all_scripts', function () { return gulp.src(['app/app.js', 'app/services/*.js', 'app/directives/*.js', 'app/components/**/*.js', 'app/routing_components/**/*.js']) .pipe(concat('all.js')) .pipe(uglify()) .pipe(gzip()) //by commenting this all.js works fine .pipe(gulp.dest('app')); }); 

Note: In my directory app/all.js.gz occur after running gulp all_scripts. But in browser its giving me error.

In browser for all.js, i am getting network response with "Accept-Encoding:gzip, deflate, br" and status 200:

 P�{P�e��*�y?R�%'S�׏���+D0�՞����v_�l��/H� ���|�! ��MrX��� O�Vg��d�K�<����}ݑ�QP��y#(R�6�e� �C�q����+1S��Y�D�鳑����G�)_"@3Y&��I�P�w@}�O�w�h[���;���!pWʂl�[�8T���R��WqI^�E�O�u�6�����������Q̩��bS��s @b�d�8>�یl�/ƛm� �02�믔�c3:η�̧eM�����zU�Q����,ζ�1��֣b�P��,�mR�\@����+G��Q� �U�9J��`]+�˺�TK����,=��f�(�O����y��C�N/�l�STS݁�P-�I���9�"#�� $�G�^͡�P=�j� ~/�!V㖣'�b�X���j ��,^F��{��6�u�y6ۢL2��b�*y�)�� ��r��C��I?�f�2re��,T�b� T��(� ��$`[Z�u�Z�$�H| �t��S7C ���R]Z�(|p��؄��S4�#]AZW� 
2
  • Have you included var gzip = require('gulp-gzip'); in your gulp script? Commented Jul 10, 2017 at 11:16
  • Yes, i have include at the top Commented Jul 10, 2017 at 11:30

1 Answer 1

1

Sounds like gzip is working fine, but your browser may not be decoding the compressed file because (I guess) your headers arent set correctly.

You need these:

Content-Type: application/javascript Content-Encoding: gzip 

See more info here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding

Sign up to request clarification or add additional context in comments.

2 Comments

Request header: Accept:*/*, Accept-Encoding:gzip, deflate, br Accept-Language:en-US,en;q=0.8
Accept-Encoding != Content-Encoding

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.