Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Frontend
  4. Creating Page-Specific CSS/JS
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia
  • Multiple Entries Per Page?

Creating Page-Specific CSS/JS

Edit this page

Warning: You are browsing the documentation for Symfony 4.0, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

Creating Page-Specific CSS/JS

If you're creating a single page app (SPA), then you probably only need to define one entry in webpack.config.js. But if you have multiple pages, you might want page-specific CSS and JavaScript.

For example, suppose you have a checkout page that has its own JavaScript. Create a new checkout entry:

1
2
3
4
5
6
7
8
9
10
// webpack.config.js

Encore
    // an existing entry
    .addEntry('app', './assets/js/main.js')
    // a global styles entry
    .addStyleEntry('global', './assets/css/global.scss')

+     .addEntry('checkout', './assets/js/checkout.js')
;

Inside checkout.js, add or require the JavaScript and CSS you need. Then, just include a script tag for checkout.js on the checkout page (and a link tag for checkout.css if you import any CSS).

Multiple Entries Per Page?

Typically, you should include only one JavaScript entry per page. This means the checkout page will include checkout.js, but will not include the app.js that's used on the other pages. Think of the checkout page as its own "app", where checkout.js includes all the functionality you need.

However, if there is some global JavaScript that you want included on every page, you can create an entry that contains that code and include both that entry and your page-specific entry. For example, suppose that the app entry above contains JavaScript you want on every page. In that case, include both app.js and checkout.js on the checkout page.

Tip

Be sure to create a shared entry to avoid duplicating the Webpack bootstrap logic and any shared modules.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Code consumes server resources. Blackfire tells you how

Code consumes server resources. Blackfire tells you how

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Peter Schultz, a Symfony contributor

Thanks Peter Schultz for being a Symfony contributor

2 commits • 34 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia