61

I'm trying to figure out the difference between url-loader vs file-loader. What does DataURl mean?

The url-loader works like the file-loader, but can return a DataURL if the file is smaller than a byte limit.

stackjlei
  • 9,485
  • 18
  • 65
  • 113

3 Answers3

75

url-loader will encode files to base64 and include them inline rather than having them loaded as separate files with another request.

A base64 encoded file may look something like this:

data:;base64,aW1wb3J0IFJlYWN0IGZ...

This would be added into your bundle.

jens
  • 2,075
  • 10
  • 15
  • 2
    what does it mean to include them inline? – stackjlei Mar 03 '18 at 03:16
  • 11
    It means that your bundle.js will have a string that represents all of the data needed to represent the asset you put through `url-loader`. That is why they let you set a limit, you don't want to base64 encode 200kb images because it will increase your bundle size by around that much. This is good for small files to reduce the number of requests made. – jens Mar 03 '18 at 03:19
  • 3
    Thanks Jens! To make sure I'm understanding this correctly, file-loader loads up your file onto the server and then provides a url, or reference, inside your bundle so your client can find it when it runs your bundle. Url-loader does the same thing, but it can ALSO make your file become PART of the bundle itself if it's small enough, correct? If I'm right, then url-loader would make file-loader obsolete, right? Since url-loader gives you more everything file-loader gives you and the option to inline files – stackjlei Mar 04 '18 at 02:10
  • 9
    The usage of file-loader vs url-loader is a matter of balance. Let's say you have a lot of very small files. Each time the browser requests this file it requires a full request/response just for the 500 bytes of .svg. With url-loader you can ensure that this file gets bundled into your .js file which has to be downloaded no matter what as one request. Larger files will get `emitted` to a directory that your web server can serve statically, this is what file-loader does. Balance size vs number of requests. Both are useful for different purposes. – jens Mar 04 '18 at 04:55
  • So file-loader in difference while create new file with new requests systematically ? – Webwoman Oct 03 '18 at 21:42
  • Not sure exactly what you are asking @Webman. file-loader will 'emit' files (named by webpack) and they will be loaded through a normal, separate HTTP request. url-loader will typically just include the file inline in the bundle. – jens Oct 04 '18 at 00:48
  • My syntax was very creative, I meant file-loader by contrast will create new file systematically while url-loader will create them only if the size limit is exceeded. I assume that this is the case if I understand your answer, thanks – Webwoman Oct 04 '18 at 00:54
  • 4
    Yes, that is correct. Within the url-loader options you can determine what that file size threshold is. Usually around 8KB. Anything larger than that will be treated like file-loader would with a separate emitted file. – jens Oct 04 '18 at 00:59
  • seems like url-loader is the better choice. what is a use-case for the latter one? – Ridhwaan Shakeel Apr 20 '19 at 00:10
49

Just wanted to add to Jens' anwer

file-loader will copy files to the build folder and insert links to them where they are included. url-loader will encode entire file bytes content as base64 and insert base64-encoded content where they are included. So there is no separate file.

They are mostly both used for media assets such as images. Mostly images.

This technique may make page load faster because there are fewer http-requests to the server to download files.

It's also important that you can specify size limit for url-loader. It will automatically fall back to file-loader for all files beyond this size:

{
    test: /\.(png|jpg|gif)$/i,
    use: [{
        loader: 'url-loader',
        options: {
            limit: 8192 // in bytes
        }
    }]
}
Gherman
  • 6,768
  • 10
  • 48
  • 75
0

Both answers are good but I want to further explain Data URL, check here https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs, so its format is

data:[<mediatype>][;base64],<data>

For example, if I convert a mp3 file, it will look like following,

import audio from '../assets/media/audio.mp3';
console.log('base64 of my audio: ', audio); 

//with url-loader setup
//I will get something like "data:audio/mpeg;base64,xxx"
Qiulang
  • 10,295
  • 11
  • 80
  • 129