Twelfth Night is one of the most commonly performed Shakesperean comedies, and was also successful during Shakespeare's lifetime. The first surviving account of the play's performance comes from a diary entry written early in 1602, talking about the play and its basic plot. The play is believed to have been written in 1601, not long after Hamlet was completed. Despite the play's initial success, it was rarely performed in the late 17th century; this unpopularity continued until the mid-18th century, when in was revived and was moderately popular until the 19th century, when the play began to fare better.
A successful production of the play from the early 19th century added a great number of songs and funny scenes lifted from other Shakespeare works; even the betrothal masque from The Tempest, which seems like it would be entirely out of place in a play like Twelfth Night, was included. The play was first performed in New York in 1804; and, in 1865, the first known production of the play with one actress performing the roles of Sebastian and Viola was staged. Of course, this development required some alteration of the text; but the experiment was later copied by Jean Anouilh, who adapted the play for French audiences.
Until the early 20th century, the play was staged in a roundly Victorian style. Sometimes, elaborate outdoor sets were constructed for the play, with the advantage of being very pretty, but with the disadvantage of all the action having to take place in that one setting. The darker, more melancholy aspects of the play were ignored in favor of broad humor and the comic set-pieces within the work; not until the 20th century did productions emphasize the tragic and bittersweet aspects of the play, and show great progress regarding insights into the characters' minds.
Although the title of the play is Twelfth Night, it is not certain that this title means that the play takes place on the "Twelfth Night" itself, or the twelfth day after Christmas. There are references within the play to Christmas, as Sir Toby drunkenly attempts something that sounds like the "Twelve Days of Christmas" song. Thematically, there are links to this period of time, which was a time of feasting and revelry; the reveling, pranks, and merriment within the play resemble activities that are characteristic of Twelfth Night, which was the culmination of the Christmas season, and a time of much festivity. Some directors of the play have taken the title quite literally, paying close attention to the Elizabethan rituals related to Twelfth Night; others have disregarded it entirely, and set the play in the sunny Mediterranean, where the historical "Illyria" is located.
The journal entry that records a performance of the piece in 1602 also compares the play to The Comedy of Errors and an Italian play named Gl'Inganni. Several 16th century Italian plays with this name survive, and all of them with the same basic plot as Twelfth Night: a woman disguises herself as a page and woos a woman for her master, whom she loves, but the woman falls in love with her, and accidentally marries her twin brother. The story was also included in two English works of prose, one written by Barnaby Riche in 1581, and the other by Emanuel Forde in 1598.
In one of the Italian versions of the play, the heroine assumes the name "Cesare" when she is in disguise, which might have been the origin of Viola's chosen name of Cesario. There is one crucial difference in the plots of the Italian versions; and that is that the heroine chooses to serve a lover who had rejected her, so the risk of recognition runs even greater. It is Riche's treatment of the tale, however, that comes closer than the Italian versions to what Shakespeare portrays in Twelfth Night, in terms of the specific situations and reactions of the characters as they interact throughout the story. However, Riche's version is not as innocent in the way the mix-up of the twins is dealt with; the Viola character reveals her gender by removing her clothes in front of the Olivia figure, and the Olivia of his work, rather than marrying Viola's twin, becomes pregnant by him and becomes involved in another confusing situation. However, Forde's portrayal of the relationship between Orsino (called Pollipus) and Viola (Violetta) is closest to Shakespeare's, in the tenderness and devotion that develops between the two characters before Violetta drops her disguise and is revealed as a woman.
It is almost certain that Shakespeare took elements of plot and character from the Italian Gl'Inganni and from Riche's and Forde's subsequent reworking of this somewhat-known story; however, Shakespeare was able to borrow elements of his previously written comedies of mistaken identity, such as The Two Gentlemen of Verona and The Comedy of Errors. In Two Gentlemen, Julia follows her love Proteus, disguised as his page, and when he falls in love with another woman, she does the wooing on his behalf. The woman she woos does not fall in love with her, however, as Olivia does with Viola. The Comedy of Errors is also a source for Twelfth Night because of the use of twins and mistaken identity in the plot; though the major difference is that the twins in Twelfth Night are a boy and a girl and therefore not completely identical, though their resemblance is used as a device in the plot. However, The Comedy of Errors is a more lighthearted work, that is more comedic in nature; Twelfth Night, though it is a comedy, delves more deeply into the grief of the twins, and into the emotional predicaments inherent in its plot.
The text of the play first appeared in the First Folio of Shakespeare's work, published in 1623. Unlike with The Tempest, there are few apparent discrepancies from what must have been Shakespeare's original text and what is published; the text does not appear to be a transcript from a performance, as the Folio text of The Tempest most likely was. There is some evidence that the text was amended by Shakespeare himself after his first performance; Viola supposedly had a song in an early version, that was cut and replaced with her story about an imaginary sister, that has bigger emotional impact. Also, the discrepancy in Orsino's title, between Count and Duke, appears to have been amended after a first performance, and Fabian's sudden substitution for Feste appears to have been done rather crudely, sometime after 1602, so that Feste could act more like an ironic commentator than merely a funny accomplice. The text of the play that has survived, however, appears to be very close to Shakespeare's original vision, and an accurate reflection of the original text, plus later additions and revisions.