Open
Conversation
<img width="280" height="280" src="https://github.com/wix-incubator/wix-code-docs/assets/91874936/08897a2d-eb67-4731-bb06-e018baeb2b6f"/><br><br> Your site's file structure is located under the `src` folder which contains the following folders: * [backend](/.wix/docs/README-backend.md) * [pages](/.wix/docs/README-pages.md) * [public](/.wix/docs/README-public.md) * [styles](/.wix/docs/README-CSS.md) <br> <h2 id="frontend-code" ><img height="32" src="https://github.com/wix-incubator/wix-code-docs/assets/91874936/129988de-e68b-432e-9337-eac1fb5410a1"> Adding frontend code to site pages</h2> <br> The `src/pages` folder contains code files for each of the pages on your site as well as the [masterpage.js](https://support.wix.com/en/article/velo-working-with-the-velo-sidebar#global-site) file. New pages you add to your site from the editor appear under this folder. Use the [$w API](https://www.wix.com/velo/reference/$w) to interact with your site's elements: ```javascript $w.onReady(function () { $w("elementId").onClick(() => { console.log("Hello World!") }) }); ``` The code you add to these files runs when visitors open pages on your site. <h3 style="font-weight:bold">Page code files</h3> When you add a page to your site in the Wix editor, a code file for that page gets added to the pages folder in your repo. The name of the file has 2 components: the name of the page that you define when you create it, and an ID string for internal use. The sections are separated by a period. <br><img width="350" height="150" src="https://github.com/wix-incubator/wix-code-docs/assets/91874936/1b50ae83-1ff8-43a3-879c-2c5c0bab789c"/><br> > **Notes:** > * Do not rename code files for pages. Wix uses these file names to associate the files with the appropriate pages on your site. > * You can't create new page code files in the Wix IDE. To add a file, create a new page for your site in the editor. <h4 style="font-weight:bold">masterpage.js</h4> The code in your `masterpage.js` runs on all pages of your site. This file is created automatically. Use this file to add code for elements that appear on all pages of your site or for general site functionality. <h3 style="font-weight:bold">Importing code from other files</h3> To import functions from other code files into your page code, using the syntax below. For public code files: ```javascript import {functionName} from 'public/myFileName'; ``` For backend code files: ```javascript import {functionName} from 'backend/myFileName.web'; ``` >**Note**: Trying to import code from the relative path in your site's files doesn't work. <br> <br> <h2 id="backend-code"><img height="32" src="https://github.com/wix-incubator/wix-code-docs/assets/91874936/d60d940c-dc1d-4cf6-8d1e-edfe3c4ef2da"> Writing backend code</h2> The `src/backend` folder contains the backend code files for your site. Backend code files for Wix sites are written in JavaScript and run using the Node.js runtime environment. <br> <h3 style="font-weight:bold">Web methods</h3> In order to call backend code from the frontend, use a special type of wrapper function called a web method within a web module. A web module is represented by a file with a `.web.js` suffix. Wix handles all the backend-frontend communication required to enable this access. <br><br> Here is an example of a web method that multiplies two numbers: ```javascript // math.web.js export const multiply = webMethod(Permissions.Anyone, (a,b) => { return a * b }); ``` This function can be then called from the frontend like this: ```javascript // Home.c1dmp.js import { multiply } from 'backend/math.web.js'; $w.onReady(function () { multiply(3,4).then((results) => { console.log(results); }) }); ``` <h3 style="font-weight:bold">Special backend files</h3> Wix supports special backend files that provide different functionality. Add the following files to the backend folder to include them in your site: + **Web Module files:** These are files that allow you to expose functions in your site's backend that you can run in your frontend code. These files require a `.web.js` file extension. + **data.js** A file for [adding data hooks](https://support.wix.com/en/article/velo-using-data-hooks) to your site's collections. + **routers.js** A file for implementing [routing and sitemap](https://support.wix.com/en/article/velo-about-routers#routing-code) functionality for your site. + **events.js** A file for implementing your site's [backend event handlers](https://support.wix.com/en/article/velo-backend-events). + **http-functions.js** A file for implementing [HTTP endpoints](https://www.wix.com/velo/reference/wix-http-functions/introduction) that are exposed on your site. + **jobs.config** A file for [scheduling recurring jobs](https://support.wix.com/en/article/velo-scheduling-recurring-jobs). Jobs consist of backend code that's run at regular intervals. + **General backend files** JavaScript code files. You can import code from these files into any other backend file on your site. These files require a `.js` file extension. <br> <br> <h2 id="public-code"><img height="32" src="./img/public.svg"> Writing public code</h2> The `src/public` folder contains the public code files for your site. You can import code from these files into any other file on your site. To import functions from other files to use in your public code files, use the following syntax. For other public code files: ```javascript import {functionName} from 'public/myFileName'; ``` For backend code files: ```javascript import {functionName} from 'backend/myFileName.web'; ``` >**Note**: Trying to import code from the relative path in your site's repo doesn't work. <br> <br> <h2 id="css-code"><img height="32" src="https://github.com/wix-incubator/wix-code-docs/assets/91874936/879fd884-50d9-49c0-8680-7e7065786a47"> Customizing your site using CSS</h2> The `src/styles/global.css` file contains the CSS code for your site. Styling with CSS allows you to completely customize the elements on your site. There are 2 ways to apply CSS styling to your site: <h3 style="font-weight:bold">1. Global classes</h3> Use global classes to apply styling to all elements of a specific type, on all site pages. CSS editing is available for [these supported classes](https://www.wix.com/velo/reference/$w/styling-elements-with-css#$w_styling-elements-with-css_available-classes). The example below show how to set a red background to all of your buttons in your site. ```css .button { background-color: red; } ``` <h3 style="font-weight:bold">2. Custom classes</h3> Use custom classs to apply style for specific elements. You can create custom classes using the CSS Classes panel, or with code using the [CustomClassList API](https://www.wix.com/velo/reference/$w/customclasslist). The example below show how to change the cursor on a button to crosshair: ```css .myCustomClass { cursor: crosshair; } ``` <br> <br> <h2 id="spi-code"><img height="32" src="https://github.com/wix-incubator/wix-code-docs/assets/91874936/2ba347da-57dd-4697-9206-4df4a5afb96e"> Adding custom extensions</h2> [Custom extensions](/.wix/docs/README-SPI.md), also known as SPIs, allow you to add your own custom logic to existing Wix app flows and to integrate services from 3rd party providers. The `src/backend/__spi__` folder contains a subfolder for each custom extension on your site. The process for implementing an SPI has these steps: 1. Create a new extension on your site using the Wix Studio Code panel. 1. Implement your extension with custom code using the Wix IDE. 1. Deploy the extension by publishing your site. Learn more about [custom app extensions using SPIs](https://support.wix.com/en/article/velo-custom-app-extensions-using-spis). <br> <br> <h2 id="concurrent"><img height="32" src="https://github.com/wix-incubator/wix-code-docs/assets/91874936/47071bd3-4294-4bb8-b487-c478067bff87"> Working in concurrent mode (Beta)</h2> Two or more site collaborators can edit a site's code at the same time in the Wix IDE. Edits made in one instance of the IDE are synced to the other instance in real time. However, you can't edit your site's code in both the Wix IDE and the Wix Studio Code panel at the same time. You also can't edit your code in the Code panel if other site collaborators are editing in the Wix IDE. When you open the Wix IDE, the Code panel switches to read-only mode. The Code panel displays this message:  If you want to edit your site's code in the Code panel, all site collaborators must close the Wix IDE. You can then click **Start Coding** in the Code panel.  <br> <blockquote class="tip"> __Tip:__ Don't forget to [publish your site](https://support.wix.com/en/article/wix-editor-saving-previewing-and-publishing-your-site#publishing-your-site) in the Wix editor to deploy your code. </blockquote>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Your site's file structure is located under the
srcfolder which contains the following folders:The
src/pagesfolder contains code files for each of the pages on your site as well as the masterpage.js file. New pages you add to your site from the editor appear under this folder. Use the $w API to interact with your site's elements:The code you add to these files runs when visitors open pages on your site.
Page code files
When you add a page to your site in the Wix editor, a code file for that page gets added to the pages folder in your repo. The name of the file has 2 components: the name of the page that you define when you create it, and an ID string for internal use. The sections are separated by a period.
masterpage.js
The code in your
masterpage.jsruns on all pages of your site. This file is created automatically. Use this file to add code for elements that appear on all pages of your site or for general site functionality.Importing code from other files
To import functions from other code files into your page code, using the syntax below.
For public code files:
For backend code files:
The
src/backendfolder contains the backend code files for your site. Backend code files for Wix sites are written in JavaScript and run using the Node.js runtime environment.Web methods
In order to call backend code from the frontend, use a special type of wrapper function called a web method within a web module. A web module is represented by a file with a `.web.js` suffix. Wix handles all the backend-frontend communication required to enable this access.Here is an example of a web method that multiplies two numbers:
This function can be then called from the frontend like this:
Special backend files
Wix supports special backend files that provide different functionality. Add the following files to the backend folder to include them in your site:
Web Module files: These are files that allow you to expose functions in your site's backend that you can run in your frontend code. These files require a
.web.jsfile extension.data.js A file for adding data hooks to your site's collections.
routers.js A file for implementing routing and sitemap functionality for your site.
events.js A file for implementing your site's backend event handlers.
http-functions.js A file for implementing HTTP endpoints that are exposed on your site.
jobs.config A file for scheduling recurring jobs. Jobs consist of backend code that's run at regular intervals.
General backend files JavaScript code files. You can import code from these files into any other backend file on your site. These files require a
.jsfile extension.The
src/publicfolder contains the public code files for your site. You can import code from these files into any other file on your site.To import functions from other files to use in your public code files, use the following syntax.
For other public code files:
For backend code files:
The
src/styles/global.cssfile contains the CSS code for your site. Styling with CSS allows you to completely customize the elements on your site.There are 2 ways to apply CSS styling to your site:
1. Global classes
Use global classes to apply styling to all elements of a specific type, on all site pages. CSS editing is available for these supported classes.
The example below show how to set a red background to all of your buttons in your site.
2. Custom classes
Use custom classs to apply style for specific elements. You can create custom classes using the CSS Classes panel, or with code using the CustomClassList API.
The example below show how to change the cursor on a button to crosshair:
Custom extensions, also known as SPIs, allow you to add your own custom logic to existing Wix app flows and to integrate services from 3rd party providers. The
src/backend/__spi__folder contains a subfolder for each custom extension on your site.The process for implementing an SPI has these steps:
Learn more about custom app extensions using SPIs.
Two or more site collaborators can edit a site's code at the same time in the Wix IDE. Edits made in one instance of the IDE are synced to the other instance in real time.
However, you can't edit your site's code in both the Wix IDE and the Wix Studio Code panel at the same time. You also can't edit your code in the Code panel if other site collaborators are editing in the Wix IDE. When you open the Wix IDE, the Code panel switches to read-only mode. The Code panel displays this message:
If you want to edit your site's code in the Code panel, all site collaborators must close the Wix IDE. You can then click Start Coding in the Code panel.